Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trim zeros after decimal point

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).

like image 803
ram Avatar asked Mar 13 '12 15:03

ram


People also ask

How do I get rid of extra digits after a decimal in Excel?

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.

How do you remove value after decimal?

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.

How do I keep the zero at the end of a decimal in Excel?

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.


1 Answers

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.

like image 90
Monolo Avatar answered Oct 07 '22 22:10

Monolo