-1

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*$//');
 printf "Your result : %d + %d = %'d\n" $bil1 $bil2 $jlh

if I input "0.1" in $bil1 and "0.4" in $bil2 , the result is

line 24: printf: 0.1: invalid number
line 24: printf: 0.4: invalid number
line 24: printf: 0.5: invalid number
Your result : 0 + 0 = 0

I want :

Your result : 0.1 + 0.4 = 0.5

how to show that result in my bash ??

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • `bash` doesn't support floats, so you have to resort to using e.g. `bc` (or some other external calculator to do the work for you). If it is an option, you can use `ksh` which supports floats natively. See also [here](http://stackoverflow.com/a/16387076/612462). – Adrian Frühwirth May 20 '13 at 14:43
  • Duplicate: http://stackoverflow.com/q/16385877/612462 – Adrian Frühwirth May 20 '13 at 14:45

3 Answers3

3

You are using %d format, which is for integers. %f is the correct format for floats. -- see the man-page for sprintf for a full reference on format-codes. %'.1f may be what you want here (thanks to Adrian for pointing that out!)

This mistaken use of %d will not actually cause printf to fail, only truncate the numbers (eg 0.4 -> 0), as you experienced.

There is also a minor problem: The second sed expression is missing the terminator (no trailing /)

kampu
  • 1,391
  • 1
  • 10
  • 14
  • but i want my bash script is for integers and floats – user2326650 May 20 '13 at 11:23
  • 1
    Perhaps you actually want to format them as strings (`%s`), since this preserves their exact content. `%d` and `%f` both reformat their input to some extent, and it seems that you want their exact text preserved. In that case you should think of them as 'strings', not as 'integers' or 'floats' – kampu May 20 '13 at 11:30
  • @kampu: The `'` in the directive is a GNU(?) extension whose meaning escapes me at the moment. – chepner May 20 '13 at 12:33
  • @chepner: I see; indeed, passing that to /usr/bin/printf doesn't complain. Apparently the zsh builtin printf was being used instead. I've edited accordingly. – kampu May 20 '13 at 12:35
  • @chepner: The `bash` manual for [Bash Builtin Commands](http://www.gnu.org/software/bash/manual/bash.html#Bash-Builtins) describes `printf` and does not mention `'` as a format modifier AFAICS. (The reference to single or double quotes refers to the non-format arguments, so `printf "%d\n" "'A"` produces 65, as does printf "%d\n" '"A'.) – Jonathan Leffler May 20 '13 at 12:43
  • @JonathanLeffler The man page refers to `standard printf(1) formats`, for which you need to look at `printf(3)`. – chepner May 20 '13 at 12:54
  • 1
    @chepner: aaahhh...for a new definition of 'standard' which means 'GNU standard' not POSIX standard...OK. – Jonathan Leffler May 20 '13 at 12:55
  • The `'` adds groupers to numbers based on the locale in use, see also [this answer](http://stackoverflow.com/a/16621380/612462). – Adrian Frühwirth May 20 '13 at 14:40
  • @AdrianFrühwirth: huh. Then that means there is actually NO format string that does exactly what they want. That seems a bit improbable. – kampu May 20 '13 at 14:47
  • @kampu There is: `%'.1f` (or similar). But they still have to do the calculation with `bc` beforehand (including a sane `scale` setting). – Adrian Frühwirth May 20 '13 at 14:50
  • @AdrianFrühwirth: not as far as I can tell. `/usr/bin/printf "%'.lf" 0.4` returns `0`, but they want `0.4` – kampu May 20 '13 at 14:53
  • @kampu Not on my system: `$ printf "%'.1f\n" 0.4` -> `0.4`. – Adrian Frühwirth May 20 '13 at 14:54
  • 1
    @kampu I think you have an `l` there instead of a `1`? – Adrian Frühwirth May 20 '13 at 14:55
  • @AdrianFrühwirth: hah. That was indeed it. – kampu May 20 '13 at 14:57
0

Use expr

jlh=expr $bil1 + $bil2 will compute $bil1 + $bil2 and set variable jlh to the same

printf "Your result : %d + %d = %d\n" $bil1 $bil2 $jlh

will print result as you want

for working with floating point numbers you need to use bc, its well explained in http://www.linuxjournal.com/content/floating-point-math-bash

varuntv
  • 11
  • 2
0

You should use bc for example. You can pipe an expression to bc and use the output of bc to print, since bash does not support floating point numbers.

I've chosen echo over printf for simplicity

#!/bin/bash

 echo -n "number 1 : ";
 read bil1
 echo -n "number 2 :";
 read bil2

 echo "$bil1 + $bil2 = $( echo "$bil1 + $bil2" | bc)"

The magick happens in the $( echo "$bil1 + $bil2" | bc) part here we pipe the expression you want to bc and use the output of bc in the echo output of at the start of that line this outputs :

number 1 : 0.1
number 2 :0.4
0.1 + 0.4 = .5

at my system but as noted by others you should install bc (if not installed ofcourse)

hetepeperfan
  • 4,292
  • 1
  • 29
  • 47