0

I want to reduce the float type precision from 7 digits to 6 after the "." I tried multiplying the number by 10 but this didn't work. Any ideas?

  • 3
    Can you please show your code? Feels like you just need formatting but not sure without see your work. – Soner Gönül Oct 17 '14 at 14:41
  • Possible duplicate: http://stackoverflow.com/questions/6356351/formatting-a-float-to-2-decimal-places – L.Butz Oct 17 '14 at 14:43
  • 4
    What are you really trying to do ? Shifting a decimal point leftwards and rightwards (which is what multiplying by any power of 10 does to a decimal number, or what multiplying by a power of 2 does to a binary number) does not affect the precision one jot. This makes me suspicious that you are using the terminology without precision. What are you trying to do ? – High Performance Mark Oct 17 '14 at 14:44

1 Answers1

2

If you're only trying to format the number on output (ie. in conversion to a string), you just need to use a proper format string:

13.651234f.ToString("f6"); // Always six decimal places

If you need to do that for your application logic, you probably want to use decimal rather than float - float is a binary number, so the notion of "decimal" decimal places is a bit off.

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • Just to say it's not about formating, i'm doing a challenge and the precision should be 6 digits after the dot. – user3868594 Oct 17 '14 at 15:36
  • @user3868594 Well, floats only have binary precision. Decimal precision is always going to be a pain. For example, `0.9f` cannot be represented in a finite binary floating point. So really, if you care about decimal precision, use `decimal` instead. – Luaan Oct 20 '14 at 08:13