Python Forum
float("{:.2f}" return long number?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
float("{:.2f}" return long number?
#1
Hello ,
I have a code that divide 2 numbers then give me 2 after the decimal point
in most cased it's working
but something I get this answer :
0.06999999999999999
0.11000000000000001
in most cases I get the correct form:
0.03
0.05
0.01
0.0
what could be the resaon ?
*** I will try to catch the 2 numbers that I'm dividing when this happand

Thanks ,
Reply
#2
(Jul-12-2022, 02:34 PM)korenron Wrote: what could be the resaon ?
Floating Point Arithmetic: Issues and Limitations
Basic Answers
It most cases it dos not matter,use f-string to control the amount of digit wanted.
>>> n = 0.06999999999999999
>>> print(f'The number is: {n:.2f}')
The number is: 0.07
>>> print(f'The number is: {n:.3f}')
The number is: 0.070
For eg financial calculation it can matter then use Decimale module
pennies = input('Enter pennies: ')
dollars = Decimal(pennies) * Decimal('.01')
print(f'{pennies} pennies is ${dollars} in dollars')
Output:
Enter pennies: 35 35 pennies is $0.35 in dollars
Reply
#3
OK
Thank you,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 314 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  Formatting float number output barryjo 2 934 May-04-2023, 02:04 PM
Last Post: barryjo
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,893 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Sequence Generator - Long number krux_rap 3 2,332 Aug-19-2020, 06:37 PM
Last Post: buran
  Factorial Code is not working when the given number is very long integer Raj_Kumar 2 2,313 Mar-31-2020, 06:40 PM
Last Post: deanhystad
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 3,398 Jul-25-2019, 11:32 PM
Last Post: scidam
  time.time wont return a useable number Nyscersul 4 3,000 Nov-15-2018, 03:08 PM
Last Post: Nyscersul
  My function won't return the sum unless the last paramater is an odd number. Maximuskiller78 1 2,333 Sep-29-2018, 03:11 AM
Last Post: ichabod801
  How to trim a float number? microphone_head 5 9,270 Aug-20-2018, 08:52 PM
Last Post: perfringo
  splitting up long number iFunKtion 7 6,219 Dec-07-2016, 05:58 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020