Python Forum
Division calcuation with answers to 1decimal place.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Division calcuation with answers to 1decimal place.
#4
some_value = 1.337
guessed_value = 1.3

# round(value, decimal_places)

if round(some_value, 1) == round(guessed_value, 1):
    print("Rounded values are equal")
If you made a calculation and want to compare if the two values are equal, you should use math.isclose.

import math


value_a = 1337 * 1.3 / 42
value_b = 1337 / 42 * 1.3

print("Value_a:", value_a)
print("Value_b:", value_b)

if value_a != value_b:
    print("The values are not equal")
    
if math.isclose(value_a, value_b):
    print("But the values are close enough")
Quote:Value_a: 41.38333333333334
Value_b: 41.38333333333333
The values are not equal
But the values are close enough

Just changing the order of calculation, will result in a different value.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Division calcuation with answers to 1decimal place. - by DeaD_EyE - Jul-15-2021, 08:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  'answers 2' is not defined on line 27 0814uu 4 790 Sep-02-2023, 11:02 PM
Last Post: 0814uu
  place 2 windows exactly above each other janeik 3 980 Jul-23-2023, 03:12 AM
Last Post: deanhystad
  Division questions Dionysis 5 1,100 Feb-14-2023, 02:02 PM
Last Post: Dionysis
  Division by zero and value of argument Lawu 5 3,206 Jul-01-2022, 02:28 PM
Last Post: Lawu
  Floor division problem with plotting x-axis tick labels Mark17 5 2,157 Apr-03-2022, 01:48 PM
Last Post: Mark17
  Cannot 'break' from a "for" loop in a right place tester_V 9 4,045 Feb-17-2021, 01:03 AM
Last Post: tester_V
  Cannot Assign right Answers To Shuffled Questions Boblows 6 2,808 Jan-22-2021, 09:41 AM
Last Post: buran
  Floor division return value Chirumer 8 3,850 Nov-26-2020, 02:34 PM
Last Post: DeaD_EyE
  Integer division plozaq 2 2,009 Sep-28-2020, 05:49 PM
Last Post: plozaq
  Overcoming ZeroDivisionError: division by zero Error dgrunwal 8 5,097 Jun-12-2020, 01:52 PM
Last Post: dgrunwal

Forum Jump:

User Panel Messages

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