0

I use this following code to round down any float to 2 decimals.

(int((number)*100))/100

For example: 112.4423 > 112.44, 100.3478 > 100.34 (not 100.35)

Yes it does the job but not for a large float with more than 16 digits (which will be transformed into exponents like this 10000000000000000.0 > 1e+16)

Is there a more practical way of round down a float which might fix this problem? and please tell me that am I providing enough information or not, Thanks.

phwt
  • 1,356
  • 1
  • 22
  • 42
  • If the numbers don't need rounding, `>=` would be the correct comparsion. So I don't see any problem. – Daniel Sep 02 '18 at 15:03
  • 1
    A better way is to use `math.trunc(number * 100) / 100`. – Daniel Sep 02 '18 at 15:05
  • Do all the numbers have more than 2 digits after decimal? – Mohd Sep 02 '18 at 15:05
  • `10000000000000000.00000` and `10000000000000000.12345` is always translated into `1e+16` rounding then always end up with no decimals (.00). – phwt Sep 02 '18 at 15:23
  • 2
    The number `10000000000000000.12345` cannot be represented with 64 bit floating point numbers. That is no problem of your rounding. – Daniel Sep 02 '18 at 15:29
  • If it can't, is there another way of getting an accurate number? – phwt Sep 02 '18 at 15:59

0 Answers0