0

Like if I have a 8.225 I want 8.22 not 8.23. DO I have to use it as a string first then convert to float? Or is there a simpler way?

Could not print the decimal without rounding off.

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • Does this answer your question? [round down to 2 decimal in python](https://stackoverflow.com/questions/41383787/round-down-to-2-decimal-in-python) – DarrylG Dec 10 '22 at 08:39

2 Answers2

0

You may take reference from below python function which shall convert a float to 2 decimal places without rounding off:

 def round2(x):
     return float(str(x)[:str(x).index('.')+3])

Alternatively, You can also convert to int after multiplying by 100 then divide again by 100 :

def round_to_2(x):
    return int(x * 100) / 100

Usually there wouldn't be any performance difference b/w two unless same operations is repeated millions of times in a loop.

trigo
  • 387
  • 1
  • 7
0

yes . you can use a string for converting. Like this : num = '8.225' num = float(num) print(type(num)) print('Float Value =', num)

I think this code can solve your problem.