Skip to content

Instantly share code, notes, and snippets.

@casjay
Created August 14, 2022 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save casjay/c521dac8752bbe02141f4ccb0d6b08cd to your computer and use it in GitHub Desktop.
Save casjay/c521dac8752bbe02141f4ccb0d6b08cd to your computer and use it in GitHub Desktop.
digit grouping
#DECIMALSEP='\.' # usa
DECIMALSEP=',' # europe
#THOUSSEP=',' # usa
#THOUSSEP='\.' # europe
#THOUSSEP='_' # underscore
#THOUSSEP=' ' # space
THOUSSEP=' ' # thinspace
# group before decimal separator
#GROUPBEFDS=4 # china
GROUPBEFDS=3 # europe and usa
# group after decimal separator
#GROUPAFTDS=5 # used by many publications
GROUPAFTDS=3
function digitgrouping {
# FIXME: This is a workaround: BEGINNING has to be marked (and after
# alteration removed) for the first number to be spaced correctly (1234
# should be 1 234, and that only works if something is in front of that
# number).
sed -e 's%^%BEGINNING&%' \
-e '
s%\([0-9'"$DECIMALSEP"']\+\)'"$THOUSSEP"'%\1__HIDETHOUSSEP__%g
:restartA ; s%\([0-9]\)\([0-9]\{'"$GROUPBEFDS"'\}\)\(['"$DECIMALSEP$THOUSSEP"']\)%\1'"$THOUSSEP"'\2\3% ; t restartA
:restartB ; s%\('"$DECIMALSEP"'\([0-9]\{'"$GROUPAFTDS"'\}\'"$THOUSSEP"'\)*\)\([0-9]\{'"$GROUPAFTDS"'\}\)\([0-9]\)%\1\3'"$THOUSSEP"'\4% ; t restartB
:restartC ; s%\([^'"$DECIMALSEP"'][0-9]\+\)\([0-9]\{'"$GROUPBEFDS"'\}\)\($\|[^0-9]\)%\1'"$THOUSSEP"'\2\3% ; t restartC
s%__HIDETHOUSSEP__%\'"$THOUSSEP"'%g' \
-e 's%^BEGINNING%%'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment