This question already has answers here:
digits after decimal point
(2 answers)
How to calculate number of digits after floating point in iOS?
(3 answers)
Closed 9 years ago.
I have some numbers that comes from server, how to know how many fraction digits in number ?
I mean how to know that 2.43 has 2 numbers after coma, 3.145 - 3 numbers, 2.0003 - 4 numbers.
Thanks in advance..
Assuming you are using a number, change it to a string. Remove the decimal point and get the last object and the length.
Example:
NSNumber *number = [NSNumber numberWithFloat:3.902];
[number.stringValue componentsSeparatedByString: #"."].lastObject.length;
I would do this :
NSNumber *number = [[NSNumber alloc] initWithFloat:3.902];
NSUInteger i = [[number stringValue] rangeOfString:#"."].location;
long numberOfDigits = [[number stringValue] length]-(i+1);
NSLog(#"%ld", numberOfDigits);
3
convert to string.
find the decimal point range.
subtract string length from the decimal point position and Add one.
Do you mean that you are retrieving the data from a server in a stream?
If your getting the data as a stream, it should be prepended with the length of the string, that is how you would know how many places to expect.
So on the server, first convert to string, then determine the length, or use another method suggested, then prepend the length of the string, and then send that number first.
Related
This question already has answers here:
How to convert a double to an int in Dart?
(11 answers)
How do you round a double in Dart to a given degree of precision AFTER the decimal point?
(28 answers)
Closed 3 years ago.
I want to round a double.
Double x = 5.56753;
x.toStringAsFixed(2);
When i put this, it gives 5.57000.
But i want to get 5.57. How do i get it?
there is num class contained function round():
Num
double numberToRound = 5.56753;
print(numberToRound.round());
//prints 6
If you want decimals
double n = num.parse(numberToRound.toStringAsFixed(2));
print(n);
//prints 5.57
check comment sujestion
For rounding doubles checkout: https://api.dartlang.org/stable/2.4.0/dart-core/double/round.html
Rounding won't work in your case because docs says:
Returns the integer closest to this.
So it will give 6 instead 5.57.
Your solution:
double x = 5.56753;
String roundedX = x.toStringAsFixed(2);
print(roundedX);
I am finding it very difficult to do a simple operation, take a large number (4 digits or above) divide by 1000, round to one decimal place and then display as a string with K but perhaps I am missing an obvious answer. (There are tons of questions on this on SO but no one seems to agree on a good answer.)
I would like the following to display as 5.6K.
int startingint = 5654;
int formatted = startingint/1000;
NString *formattedstr = [NSString stringWithFormat:#"%dK", formatted];
Instead, it displays 5K.
Can anyone suggest how to get it to show an extra decimal place?
Thanks in advance for any suggestions.
You can try
float starting = 5654.0;
float formatted = starting/1000.0;
NString *formattedstr = [NSString stringWithFormat:#"%.1fK", formatted];
Hi everyone,
I'm working with a private API and I need to send integer and double values.
For integers, I don't have any problem, I convert the integer to NSNumber and everything works fine.
But with double with no decimal numbers ( 46 for instance ) my request is rejected because the server sees an integer where there should be a double.
My sys admin told me to send round double value with ".0", so if I want to send the double 46, I have to send 46.0.
The problem is that I can't send an NSString or the server will also reject my request ( it will see a string where there should be a double ).
So here is my question : is there a way to add representative numbers to NSNumbers ? So my double 46 would be NSNumber 46.0
Can anyone help me ?
Thanks in advance.
When ever you are initialising the NSNumber you should use
mynumber = [NSNumber numberWithDouble: myDoublevalue];
and when you want to send it to server try like
[mynumber doubleValue];
I guess the question is: What do you use to create your NSNumber?
Using
NSNumber *intNumber = #(46);
Will give you an NSNumber that contains an int, whereas
NSNumber *doubleNumber = [NSNumber numberWithDouble:46];
should result in an NSNumber containing a double .
(You can check this by calling [intNumber objCType], which will give "i" and [doubleNumber objCType will give "d")
The other problem you might experience is that you (or the API you use) uses some format conversion to JSON or something else.
Normal JSON converters will omit the .0 and therefore you might get an error.
Therefore if you have access to the JSON (or what ever else format conversion you use, since you will never send an NSNumber to any server), you can fix it there.
Last point, if nothing helps with the two points above, you could think about adding a very small number, that will force a point, but won't make much of a difference in normal cases:
input = pow(2,log2(fabs(input))-50) //use 20 instead of 50 for float numbers!
NSNumber *result = #(input);
This question already has answers here:
NSString intValue not working for retrieving phone number
(2 answers)
Closed 8 years ago.
Hi when I try to save a phonenumber with this line to a coredata element
[newLead setValue:[NSNumber numberWithInteger:[self.cellTextField.text doubleValue] ] forKey:#"cell"];
Then I load it with this
[self.cellTextField setText:[[self.lead valueForKey:#"cell"] stringValue]];
It works but if the number is like 905666777 , that number is too big and i get 2147483647
How can i make it use a 905 number?
also when i dial the number using
NSURL *telUrl = [NSURL URLWithString:[#"tel://" stringByAppendingString:#"cell"]];
[[UIApplication sharedApplication] openURL:telUrl];
It only dials the first 3 digits 214, how can i make it dial all the digits
Thank you
The problem is that you're saving a phone number as a numeric value, so you're running into limits on numeric types. There's no reason to do this this-- you should be using a string. You'll never do math on the phone number, for example. Any fetches you might do (for example finding phone numbers with a specific area code) will be more difficult with a numeric type than with a string.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I round a float value to 2 post decimal positions?
Lets say I have a double number of 3.46.
How do I round it to 3.50?
I tried
NSLog(#"res: %.f", round(3.46));
but it return 3.
Do some calculations....
float f=3.46;
float num=f+0.05;//3.51
int intNum=num*10;//35
float floatNum=intNum/10.0;//3.5
NSLog(#"res: %.2f", floatNum); //3.50
Following code may help
i = roundf(10 * i) / 10.0;
where i is your float variable
If you're willing to live with the rounding rules from printf, then the following should suffice when rounding for presentation:
NSLog(#"res: %.1f0", 3.46);
Note that the 0 is just a normal character that is added after the value is formatted to the appropriate number (1) of decimal places. This approach should be usable with [NSString stringWithFormat:] as well.
The original code results in "3" because round always returns an integral value.
YMMV; I don't even use iOS.