4

I have a double thats got a value of something like 0.50000 but I just want 0.5 - Is there any way to get rid of those trailing 0's? :)

Chuck
  • 234,037
  • 30
  • 302
  • 389
tarnfeld
  • 25,992
  • 41
  • 111
  • 146

2 Answers2

13

In C,

printf("%g", 0.5000);

Note: (from GNU libc manual)

The %g and %G conversions print the argument in the style of %e or %E (respectively) if the exponent would be less than -4 or greater than or equal to the precision; otherwise they use the ‘%f’ style. A precision of 0, is taken as 1. Trailing zeros are removed from the fractional portion of the result and a decimal-point character appears only if it is followed by a digit.

N 1.1
  • 12,418
  • 6
  • 43
  • 61
  • It works fine but it gives Exponential form for large numbers & i don't want that.. Any solution to that? – Sneha May 27 '16 at 12:29
6

standard c format statements.

NSLog(@" %.2f", .5000)
Brian Postow
  • 11,709
  • 17
  • 81
  • 125