JustToThePoint English Website Version
JustToThePoint en español
JustToThePoint in Thai

How to convert units

One accurate measurement is worth a thousand expert opinions, Grace Hopper.

Unit conversion is the process of converting a quantity from one base or unit of measurement to another by multiplying or dividing it by a conversion factor. It expresses the quantity of a property as a different unit of measurement, e.g., time can be expressed in minutes or seconds instead of hours (2 hours = 2*60 minutes = 120 minutes = 120*60 seconds = 7200 seconds), distance or length can be converted from miles to feet (0.5 miles = 0.5 * 1609.34 meters = 804.672 meters), and volumen is usually measured in liters and gallons (5 liters = 5 * 0.264172 gallons = 1.32086 gallons). How to convert units

To install Pint, simply: python3 -m pip install pint.

user@pc:~$ python
Python 3.9.5 (default, May 11 2021, 08:20:37) 
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pint import UnitRegistry
>>> ureg = UnitRegistry() # It initializes a UnitRegistry() object. It stores the unit definitions, their relationships, and handles conversions between units.
>>> ureg.default_format = '.2f' # It defines the default formatting string, it rounds the result to two decimal places.
>>> temperature = ureg.Quantity(29, ureg.degC) # It defines a Quantity object (it describes a physical quantity) with a value (29) and the units of the physical quantity (Celsius).
>>> print(temperature.to('degF')) # It convert from Celsius to Fahrenheit.
84.20 degree_Fahrenheit
>>> distance = 10 * ureg.kilometer # distance is a pint.util.Quantity(10, 'kilometer') object
>>> print(distance.to(ureg.miles)) # It converts from kilometers to miles.
6.21 mile
>>> time = 7.0 * ureg.minutes # time is a pint.util.Quantity(7.0, 'minute') object
>>> print(time.to(ureg.second)) # It converts from minutes to seconds.
420.00 second
>>> speed = ureg.Quantity(45, 'm/seconds')
>>> print(speed.to('in/hour'))
6377952.76 inch / hour
Bitcoin donation

JustToThePoint Copyright © 2011 - 2024 Anawim. ALL RIGHTS RESERVED. Bilingual e-books, articles, and videos to help your child and your entire family succeed, develop a healthy lifestyle, and have a lot of fun. Social Issues, Join us.

This website uses cookies to improve your navigation experience.
By continuing, you are consenting to our use of cookies, in accordance with our Cookies Policy and Website Terms and Conditions of use.