-2

As I said on the title, I have a decimal number such as "1.512411". I want to shorten this decimal number into two digits, I mean it should be "1.51". I searched for it on internet for 2 days but I could find nothing about this. If there is a way to do it, can you explain me how i can do that? If you explain it detailed, I'll be so happy.

  • 2
    Does this answer your question? [Round number to nearest integer](https://stackoverflow.com/questions/31818050/round-number-to-nearest-integer) – cabad Jul 13 '22 at 14:21
  • @cabad oh, I forgot to say in the post! I have already tried this but it does not give me the result that I exactly want from it. – Bugra Pakir Jul 13 '22 at 14:22
  • "Shortening to two digits" is referred to in english as "rounding". This is one of those very common things that if you don't know the right word, is so hard to search. Best of luck! If that doesn't do what you want, please explain why "rounding" doesn't give the result you want. – JNevill Jul 13 '22 at 14:22
  • You mean two decimal digits? Rounding or truncating? – Tim Pietzcker Jul 13 '22 at 14:22
  • 1
    Sounds like multiplying by 100, then rounding to integer and dividing by 100 should do the trick. And if you worry about floating point precision issues, maybe use Decimals? – Tim Pietzcker Jul 13 '22 at 14:24
  • I guess it is truncating. – Bugra Pakir Jul 13 '22 at 14:24
  • 2
    @Bugra Pakir if the number was 2.3456, would you want to see "2.34" (truncating) or "2.35" (rounding)? – slothrop Jul 13 '22 at 14:25
  • 2
    If you want the result as a string you can use `"{:.2f}".format(number)` – mousetail Jul 13 '22 at 14:26
  • 2
    @slothrop yeah I wanna see "2.34", it's truncating I guess. I'm so sorry for using incorrect word. My native language is not English. – Bugra Pakir Jul 13 '22 at 14:26
  • @mousetail I've never seen this structure. Can you explain me how it exactly does or send me a video about this? – Bugra Pakir Jul 13 '22 at 14:28
  • @BugraPakir More info [here](https://docs.python.org/3.8/library/string.html#formatspec) and [here](https://docs.python.org/3.8/library/functions.html#format) – mousetail Jul 13 '22 at 14:29
  • 2
    Does this answer your question? [Python Decimals format](https://stackoverflow.com/questions/2389846/python-decimals-format) – mousetail Jul 13 '22 at 14:30
  • @mousetail I solved the problem thanks to you. By doing "{:.2f}".format(number) – Bugra Pakir Jul 13 '22 at 14:35
  • @Bugra Pakir be careful - that does rounding not truncation. For example `"{:.2f}".format(2.3456)` gives `2.35`. – slothrop Jul 13 '22 at 14:38

3 Answers3

2

There is a simple way to do this, using the function round(value, n_digits) function. This is the corresponding documentation.

Behavior:

[In]  round(1.512411, 2)
[Out] 1.51
Kins
  • 547
  • 1
  • 5
  • 22
0

You can use round(number[, digits]) function.

>>> round(1.512411, 2)
1.51
>>> round(1.515, 2)
1.51
>>> round(1.516, 2)
1.52
ventaquil
  • 2,780
  • 3
  • 23
  • 48
Dirk
  • 129
  • 1
  • 4
0

I solved the problem by using F-String. You guys need to do for truncating the decimal numbers:

a = 1.516171
print(f"{a:.2f}") # You need to type the number which you want to truncate.
  • This does rounding rather than truncation. For example if a=2.3456, this will print `2.35` not the `2.34` that you want. In fact, even for your example here, it gives you `1.52` (rounded) not `1.51` (truncated). – slothrop Jul 13 '22 at 14:45