How to separated zero with point in my bash script

2019-09-27 11:39发布

My script

 echo -n "number 1 : ";
 read bil1
 echo -n "number 2 :";
 read bil2
 jlh=$(echo $bil1 + $bil2 |bc -l |sed -e 's/^\./0./' -e 's/^-\./-0' -e 's/\.0*$//');
 echo " Your result : $bil1 + $bil2 = $jlh";

if I input " 100000 " in $bil1 and "100000 " in $bil2 , the result is " 200000 ".

I want :

Your result : 100000 + 100000 = 200.000

how to separated zero with " . " for that result from 200000 to 200.000 ?

1条回答
Ridiculous、
2楼-- · 2019-09-27 11:44

If your libc supports it, the ' flag in a printf specifier will add groupers to a number.

$ LC_NUMERIC=en_US printf "%'d\n" 100000
100,000
$ LC_NUMERIC=fr_FR printf "%'d\n" 100000
100 000
$ LC_NUMERIC=de_DE printf "%'d\n" 100000
100.000
$ LC_NUMERIC=bn_IN printf "%'d\n" 100000
1,00,000
查看更多
登录 后发表回答