Register a domain and help support LQ
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Convert Metric <-> Imperial

Notices

- - - - - As of February 1st 2014 this blog is no longer updated - - - - -

# Convert Metric <-> Imperial

Posted 01-03-2014 at 05:14 AM by druuna
Updated 01-31-2014 at 04:24 AM by druuna
Tags

Converting from metric to imperial and vice versa using bc.

Here are a few examples in the form of functions:

# -------------------------------------------------------------------------- #
# - Imperial to metric
# -------------------------------------------------------------------------- #

• pounds to kilograms
PHP Code:
``` # Function : lbToKg # Syntax   : lbToKg <pounds> # # Convert pounds to kilogram function lbToKg() {   echo "\$1 * 0.4536" | bc }  ```
• ounces to grams
PHP Code:
``` # Function : ozToGr # Syntax   : ozToGr <ounces> # # Convert ounces to grams function ozToGr() {   echo "\$1 * 28.35" | bc }  ```
• inches to centimeters
PHP Code:
``` # Function : inToCm # Syntax   : inToCm <inches> # # Convert inches to centimeters function inToCm() {   echo "\$1 * 2.54" | bc }  ```
• feet to meters
PHP Code:
``` # Function : ftToMe # Syntax   : ftToMe <feet> # # Convert feet to meters function ftToMe() {   echo "\$1 * 0.3048" | bc }  ```
• miles to kilometers
PHP Code:
``` # Function : miToKm # Syntax   : miToKm <miles> # # Convert miles to kilometers function miToKm() {   echo "\$1 * 1.6093" | bc }  ```
• yards to meters
PHP Code:
``` # Function : ydToMe # Syntax   : ydToMe <yards> # # Convert yards to meters function ydToMe() {   echo "\$1 * 0.9144" | bc }  ```
• feet and inches to centimeters
PHP Code:
``` # Function : fiToCm # Syntax   : fiToCm <feet> <inches> # # Convert feet and inches to centimeters function fiToCm() {   echo "scale=0 ; ((\$1 * 30.48) + (\$2 * 2.54)) / 1" | bc }  ```
• fahrenheit to celsius
PHP Code:
``` # Function : faToCe # Syntax   : faToCe <temp_fahrenheit> #  C = (F - 32) x 5/9 # Convert fahrenheit to celsius function faToCe() {   echo "scale=2 ; (\$1 - 32) * 5/9" | bc }  ```
# -------------------------------------------------------------------------- #
# - Metric to Imperial
# -------------------------------------------------------------------------- #

• kilograms to pounds
PHP Code:
``` # Function : kgToLb # Syntax   : kgToLb <kilos> # # Convert kilograms to pounds function kgToLb() {   echo "\$1 * 2.2046" | bc }  ```
• grams to ounces
PHP Code:
``` # Function : grToOz # Syntax   : grToOz <grams> # # Convert grams to ounces function grToOz() {   echo "\$1 * 0.0353" | bc }  ```
• centimeters to inches
PHP Code:
``` # Function : cmToIn # Syntax   : cmToIn <centimeters> # # Convert centimeters to inches function cmToIn() {   echo "\$1 * 0.3937" | bc }  ```
• meters to feet
PHP Code:
``` # Function : meToFt # Syntax   : meToFt <meters> # # Convert meters to feet function meToFt() {   echo "\$1 * 3.2808" | bc }  ```
• kilometers to miles
PHP Code:
``` # Function : kmToMi # Syntax   : kmToMi <kilometers> # # Convert kilometers to miles function kmToMi() {   echo "\$1 * 0.6214" | bc }  ```
• meters to yards
PHP Code:
``` # Function : meToYd # Syntax   : meToYd <meters> # # Convert meters to yards function meToYd() {   echo "\$1 * 1.0936" | bc }  ```
• centimeters to feet and inches
PHP Code:
``` # Function : cmToFi # Syntax   : cmToFi <centimeters> # # Convert centimeters to feet and inches function cmToFi() {   FEET=\$( echo "scale=0;(\$1 * 0.3937) / 12" | bc )   FEETTOT=\$( echo "scale=2;(\$1 * 0.3937) / 12" | bc )   INCHES=\$( echo "scale=0;((\$FEETTOT - \$FEET) * 12 + 0.5 )/1" | bc )   echo "\$FEET \$INCHES" }  ```
• celsius to fahrenheit
PHP Code:
``` # Function : ceToFa # Syntax   : ceToFa <temp_celsius> #  F = (C x 9/5) + 32 # Convert celsius to fahrenheit function ceToFa() {    echo "scale=2 ; (\$1 * 9/5) + 32" | bc }  ```
Posted in General
« Prev     Main     Next »

All times are GMT -5. The time now is 02:42 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -