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.
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
Can someone explain to me why the following sum gives wrong result in dart?
final double result = 90071992547409.9 + 0.01;
print(result);
It prints the number 90071992547409.92
According to Dart documentation:
Dart doubles are 64-bit floating-point numbers as specified in the IEEE 754 standard.
It's because of floating-point arithmetic. In your case (I used this converter):
90071992547409.9 = 90071992547409.90625 ~= 90071992547409.91
0.01 = 0.01000000000000000020816681711721685132943093776702880859375 ~= 0.01
90071992547409.91 + 0.01 = 90071992547409.92
The best solution in dart is to use the decimal package.
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];
I am trying to round big numbers to the nearest 1 hundred, by dividing by 100, rounding and then multiplying by 100.
However, the big number values are losing value when converting to floats.
This is the problem part:
int bigInt = 99222049;
float bigFloat = bigInt / 100.0;
NSLog(#"%d %f", bigInt, bigFloat);
Output:
99222049 992220.500000
Would like it to be:
99222049 992220.49
Sorry if this is a stupid question! Would be grateful for advice. Thanks.
Your basic problem is float rounding. You cannot express arbitrary decimal fractions as floats (or doubles). In binary, 1/10 is 0.00011001100110011... It never terminates, just like 1/3 is 0.33333... in decimal. So you need to do your work in base 10, not base 2, if you want your results to round nicely in base 10. There are two approaches in Cocoa:
You can do your work purely with integer math, no floating point. See Rounding integers to nearest ten or hundred in C for an example of how to do this.
Alternately, since this is ObjC, you can use NSDecimalNumber, which will allow you to perform all your math in base 10.
You need to change your float to a double.
int bigInt = 99222049;
double bigFloat = bigInt / 100.0;
NSLog(#"%u %f", bigInt, bigFloat);
Outputs:
99222049 992220.490000
Change your code to this and the output should show as you desire. I changed the float to a double and .2 before f to show only 2 decimal places.
int bigInt = 99222049;
double bigFloat = bigInt / 100.0;
NSLog(#"%d %.2f", bigInt, bigFloat);
This is what shows in console
99222049 992220.49
I hope this helps, cheers, Jim.
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.