0

I would like to truncate a number with only two positions after decimal point and return this number. But when I use the string format as float(" % .2f" % number), it returns 1.0 when the given number is like 1.0000. How could I resolve this problem? Thanks a lot.

It seems like my description was not clear enough. Let me take the 1.0005 as an example. I would like to return the number 1.00 with only two positions after the decimal point, rather than a string "1.00". So I choose to use string format to truncate the original number 1.0005 to a string "1.00" which satisfies the requirement of decimal points but it is still a string, I would like to return a number. So I use the float to convert it to a float number, but it seems like float() in gonna truncate the number to 1.0.

Aurora
  • 423
  • 2
  • 5
  • 12
  • Hi Prune, thanks for noticing. I have updated new description above. – Aurora Oct 26 '18 at 22:04
  • Mathematically speaking, 1.0 is equal to 1.000. There's no "truncation" happening there: whether the value is represented as a string as `1` or `1.0` or `1.000` is entirely a matter of formatting, not of the value itself. More importantly, `float` objects use the IEEE 754 double-precision storage format internally, which does not provide any way of distinguishing numbers by representation, only by value. – Daniel Pryden Oct 27 '18 at 00:16
  • Oops, I got it. Thanks for your help! – Aurora Oct 27 '18 at 01:31

0 Answers0