Skip to content

Instantly share code, notes, and snippets.

@thcipriani
Created June 15, 2022 20:19
Show Gist options
  • Save thcipriani/915b47d59308db2bd9ea8a70e6719b1c to your computer and use it in GitHub Desktop.
Save thcipriani/915b47d59308db2bd9ea8a70e6719b1c to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
printf '%s°C\n' "$(units "tempF($@)" 'tempC' | xargs)"
printf '%s°F\n' "$(units "tempC($@)" 'tempF' | xargs)"
@tresni
Copy link

tresni commented Jun 17, 2022

This should be portable for anything with ZSH

#!/usr/bin/env zsh

# Celsius is normally represented with 1 decimal place
printf '%.1f°C\n' "$((($@-32)*5.0/9.0))"
# Fahrenheit is generally represented as a whole number
printf '%.0f°F\n' "$(($@*9.0/5.0+32))"

Bash doesn't do floating point, so another option would be bc which generally comes with most linux distros and macos.

#!/usr/bin/env bash

set -euo pipefail

# Celsius is normally represented with 1 decimal place
printf '%.1f°C\n' "$(bc <<< "scale=2;($@-32)*5.0/9.0")"
# Fahrenheit is generally represented as a whole number
printf '%.0f°F\n' "$(bc <<< "scale=2;$@*9.0/5.0+32")"

@brandonpittman
Copy link

It should be noted that this doesn't work with macOS' built-in units. It will work with gnu-units though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment