I am trying to trim zeros after a decimal point as below but it's not giving desired result.
trig = [currentVal doubleValue];
trig = trig/100;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:0];
display.text = [formatter stringFromNumber:[NSNumber numberWithDouble:trig]];
The number is still being displayed without trimming zeros after the decimal point.
Here currentVal
is the number I am entering.
For example if i pass "trig" = 123 (Initially "trig" = 123 after doing trig/100 i want to display 1.23 but it is displaying as 1.23000000).
By using a button: Select the cells that you want to format. On the Home tab, click Increase Decimal or Decrease Decimal to show more or fewer digits after the decimal point.
For example, to remove all digits except the first one after decimal, you can apply the formula =INT(E2*10)/10. TRUNC function: Besides the value you will remove digits after decimal, enter the formula =TRUNC(E2,0) into a blank cell, and then drag the Fill Handle to the range you need.
Add an apostrophe before the number. Edit each cell to start with an apostrophe ('), then type the number exactly as it should appear. The apostrophe tells Excel to store this cell as text, meaning it cannot be used for formulas.
Sometimes the straight C format specifiers do an easier job than the Cocoa formatter classes, and they can be used in the format string for the normal stringWithFormat:
message to NSString
.
If your requirement is to not show any trailing zeroes, then the "g" format specifier does the job:
float y = 1234.56789f;
NSString *s = [NSString stringWithFormat:@"%g", y];
Notice that there is no precision information, which means that the printf
library will remove the trailing zeroes itself.
There is more information in the docs, which refer to IEEE's docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With