Skip to content

Instantly share code, notes, and snippets.

@thcipriani
Created Jun 15, 2022
Embed
What would you like to do?
#!/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

brandonpittman commented Jun 18, 2022

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