-2

I want to take a float that can be entered by the user to any acceptable number of decimal places and format it so that it is only two decimal places. I am able to do this by converting it to a String but I want to keep it as a float and was wondering if there was an easier way to do this than changing from float to string and then back to a float?

Naman
  • 23,555
  • 22
  • 173
  • 290

1 Answers1

0

Here's a way to do it: https://stackoverflow.com/a/153785/4119650 It's a little more complicated than the String conversion method, but it gives you greater control over the truncation.