Skip to content

Instantly share code, notes, and snippets.

@cmod
Created June 25, 2025 09:27
Show Gist options
  • Save cmod/a8420254f130ed11a3379120c025a8ed to your computer and use it in GitHub Desktop.
Save cmod/a8420254f130ed11a3379120c025a8ed to your computer and use it in GitHub Desktop.
JPY USD Conversion (usable with Alfred)
#!/bin/bash
query="$1"
# Simple function to add commas using Python (most reliable)
add_commas() {
python3 -c "print(f'{float('$1'):,.2f}')"
}
if [[ $query =~ ^([0-9]+\.?[0-9]*)$ ]]; then
amount="$1"
# Get exchange rate
rate=$(curl -s "https://api.exchangerate-api.com/v4/latest/USD" | grep -o '"JPY":[0-9.]*' | cut -d':' -f2)
if [[ -z "$rate" ]]; then
rate="145.0"
fi
# Calculate conversions
usd_to_jpy_raw=$(echo "$amount * $rate" | bc -l)
jpy_to_usd_raw=$(echo "scale=4; $amount / $rate" | bc -l)
# Format with commas
amount_fmt=$(add_commas "$amount")
usd_to_jpy_fmt=$(add_commas "$usd_to_jpy_raw")
jpy_to_usd_fmt=$(add_commas "$jpy_to_usd_raw")
# Output JSON
cat << EOF
{
"items": [
{
"uid": "usd-to-jpy",
"title": "\$$amount_fmt USD = ¥$usd_to_jpy_fmt JPY",
"subtitle": "Press Enter to show both conversions on screen",
"arg": "\$$amount_fmt USD = ¥$usd_to_jpy_fmt JPY\n¥$amount_fmt JPY = \$$jpy_to_usd_fmt USD\n\nRate: 1 USD = ¥$rate JPY"
},
{
"uid": "jpy-to-usd",
"title": "¥$amount_fmt JPY = \$$jpy_to_usd_fmt USD",
"subtitle": "Press Enter to show both conversions on screen",
"arg": "\$$amount_fmt USD = ¥$usd_to_jpy_fmt JPY\n¥$amount_fmt JPY = \$$jpy_to_usd_fmt USD\n\nRate: 1 USD = ¥$rate JPY"
},
{
"uid": "rate",
"title": "Rate: 1 USD = ¥$rate JPY",
"subtitle": "Press Enter to show both conversions on screen",
"arg": "\$$amount_fmt USD = ¥$usd_to_jpy_fmt JPY\n¥$amount_fmt JPY = \$$jpy_to_usd_fmt USD\n\nRate: 1 USD = ¥$rate JPY"
}
]
}
EOF
else
cat << EOF
{
"items": [
{
"uid": "usage",
"title": "Enter a number for USD/JPY conversion",
"subtitle": "Example: j 100",
"valid": false
}
]
}
EOF
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment