-5

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 ?

brasofilo
  • 25,496
  • 15
  • 91
  • 179
  • http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript/2901298#2901298 shows how to do it in Javascript, but I don't think `sed` supports the regexp extensions that this depends on. – Barmar May 18 '13 at 06:17
  • Not a duplicate; in that question, he wanted to remove a trailing decimal portion. In this question, he seems to want a grouping character added to the result. – chepner May 18 '13 at 14:24
  • your sed command will change `-.333` to `-0333` – glenn jackman May 18 '13 at 17:53

1 Answers1

3

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
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358