+ 1

Is there any other way of rounding off numbers without using 'round' ?

23rd Nov 2022, 3:30 PM
Levin Mwanganyi
Levin Mwanganyi - avatar
7 Antworten
+ 10
Levin Mwanganyi , as Lisa already mentioned, you can use an f-string or an other string formatting to get a defined number of decimal places from a float number. in this case a rounding is performed. but there are also cases where you need to reduce the number of digital places but just cutting without any rounding: n = 3 # number of decimal places x = 1.1667 # this version is cutting to n decimal places included a rounding print(f"{x:.{n}f}") # res: 1.167 # this version is cutting to n decimal places WITHOUT any rounding print(int(x * (10**n))/(10**n) ) # res: 1.166
23rd Nov 2022, 7:48 PM
Lothar
Lothar - avatar
+ 8
# string formatting x = 1.2345 print(f"{x:.2f}")
23rd Nov 2022, 5:20 PM
Lisa
Lisa - avatar
+ 5
Depends on the language. (Might be a good idea to put the language in the tag)
23rd Nov 2022, 3:59 PM
Ausgrindtube
Ausgrindtube - avatar
+ 2
Do you mean "rounding" ("mathematically") or "rounding off" in terms of cutting off the decimals? For the later, converting the number to an integer might be an option.
23rd Nov 2022, 4:02 PM
Lisa
Lisa - avatar
+ 2
I mean reducing the number of decimal places from let's say 4 to 2 decimal places
23rd Nov 2022, 4:59 PM
Levin Mwanganyi
Levin Mwanganyi - avatar
0
Thank you
23rd Nov 2022, 5:24 PM
Levin Mwanganyi
Levin Mwanganyi - avatar
0
Like the posts before double the expression symbols (**, //) will show up the value and cut decimalplaces
25th Nov 2022, 2:33 PM
S3R43o3
S3R43o3 - avatar