Skip to content

Instantly share code, notes, and snippets.

@drocco007
Last active February 22, 2021 02:49
Show Gist options
  • Save drocco007/f6440b24a573c0ce781db412c9ca18e7 to your computer and use it in GitHub Desktop.
Save drocco007/f6440b24a573c0ce781db412c9ca18e7 to your computer and use it in GitHub Desktop.
Compare GNU numfmt to uutils/coreutils numfmt
#!/usr/bin/bash
PAD=10
TMPL="%${PAD}s %${PAD}s %${PAD}s %s\n"
SEP=$(printf "_%.0s" $(seq 1 $PAD))
# Format the values in a table for easy comparison
#
# $1 header label (SI or IEC)
# $2 labels (first column)
# $3 gnu values (second column)
# $4 uu values (third column)
print_table () {
printf "$TMPL" "" "UNIT: $1 "
printf "$TMPL" "input" "GNU numfmt" "uu numfmt"
echo "$SEP $SEP $SEP"
labels=($2)
gnu=($3)
uu=($4)
for i in $(seq 0 ${#labels[@]}) ; do
matches=$([ ${gnu[$i]} == ${uu[$i]} ] && echo "" || echo "×")
printf "$TMPL" ${labels[$i]} ${gnu[$i]} ${uu[$i]} $matches
done
}
si='-1001
-999.1
-999
1
500
999
999.1
1000
1000.1
1001
9900
9901
9949
9950
9951
10000
10001
10500
10999
50000
99000
99001
99900
99949
99950
100000
100001
100999
101000
101001
999000
999001
999949
999950
999999
1000000
1000001
999000000.1
999000001'
gnu=$(echo "$si" | numfmt --to si)
uu=$(echo "$si" | target/debug/numfmt --to si)
print_table "SI" "$si" "$gnu" "$uu"
echo
iec='-1025
-1024.1
-1024
-1023.1
-1023
1
1023
1024
1025
1126
1127
1536
1537
1945
1946
1996
1997
2048
2049
10188
10189
10240
102348
102349
102400
971776
972288
972800
972801
973824
973825
1013760
1013761
1018879
1018880
1018881
1019904
1019905
1022976
1022977
1047552
1047553
1048575
1048576
1048577
1073741823
1073741824
1073741825'
gnu=$(echo "$iec" | numfmt --to iec)
uu=$(echo "$iec" | target/debug/numfmt --to iec)
print_table "IEC" "$iec" "$gnu" "$uu"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment