Python Forum
Define unit of measure of a number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Define unit of measure of a number
#1
I would like a function or command to define the unit of measure, either MPa (10 ^ 6) or GPa (10 ^ 9).e.g
20,000,000,000.00 > 20GPa
5,000,000.00 > 5MPa
Reply
#2
And how might you expect that to look like in code?
Reply
#3
def transform(x):
    if x < gpa:
        return(str(int(x/mpa))+"MPa")
    elif x >= gpa:
        return(str(int(x/gpa))+"GPa")

gpa=1000000000
mpa=1000000
value1= 20000000000
value2= 5000000
print(f"{value1} > {transform(value1)}")
print(f"{value2} > {transform(value2)}")
Output:
20000000000 > 20GPa 5000000 > 5MPa
Reply
#4
This would also be simple enough to do as a lambda function:
g = lambda x : str(x/1000000)+' MPa' if x<1000000000 else str(x/1000000000)+' GPa'

value1= 20000000000
value2= 5000000

print (g(value1))
print (g(value2))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unit Testing Set Up and Use RockBlok 2 422 Jan-08-2024, 07:43 PM
Last Post: deanhystad
  How to measure execution time of a multithread loop spacedog 2 2,860 Apr-24-2021, 07:52 AM
Last Post: spacedog
  Remove function and unit test ftg 5 3,528 Jan-07-2020, 03:10 PM
Last Post: ndc85430
  Odd Unit Test Behavior ichabod801 3 2,572 Jan-02-2020, 03:34 PM
Last Post: ichabod801
  Unit testing - AssertRaises kerzol81 3 4,557 Oct-07-2019, 10:35 AM
Last Post: buran
  Measure accuracy of Object Detection Shameendra 2 2,678 Nov-19-2018, 01:04 PM
Last Post: Shameendra
  unit test roll die saladgg 5 4,161 Nov-06-2018, 11:39 PM
Last Post: stullis
  How to measure an inclined beam width and height in image using python? zyb1003 1 3,218 Nov-07-2017, 05:02 AM
Last Post: heiner55
  Would you unit test __init__ method? kilthar 1 30,983 Oct-18-2017, 05:31 PM
Last Post: snippsat
  Unit testing mp3909 1 2,637 Oct-15-2017, 03:48 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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