0

I'm stuck on this USD to SEK currency converter and the empty row, row 12. The current conversion is 1 SEK = 0.158193 USD 1 USD = 6.32138 SEK.

Row 12 will have something like SEK="(?(USD) )?"

I don't know what to enter in the question marks.

#!/bin/bash

shopt -s -o nounset
declare -i USD # USD
declare -i SEK # SEK
# Title
printf "%s\n" "USD-SEK Currency Convertor"
printf "\n"
# Get the value to convert
read -p "Enter a USD: " USD
# Do the conversion

printf "You will get SEK %d\n" "$SEK"
exit 0
  • possible duplicate of [Multiplication on command line terminal UNIX](http://stackoverflow.com/questions/11039876/multiplication-on-command-line-terminal-unix) – Marc B Feb 14 '13 at 16:14

1 Answers1

1

You can do floating point arithmetic using bc like this:

SEK=$( echo " 6.32138 * $USD " | bc -l )

Explanation:

Bash does not have built it support for floating point arithmetic. Thus, we usually handle these operation using the bc program. bc reads an arithmetic expression as a string from the standard input, and prints the result the standard output. Note that the -l option is necessary for keeping the decimal part of the expression.

In order to get the result from bc, and store it in a variable, we use command redirection i.e. the $( ). Note that there are no spaces before and after the = in the previous expression.

Complete Example

#!/bin/bash
printf "%s\n" "USD-SEK Currency Convertor"
# Get the value to convert
read -p "Enter a USD: " USD
SEK=$(echo " 6.32138 * $USD " | bc -l )
printf "You will get SEK %s\n" "$SEK"  ;#  NOTE THAT I CHANGED THIS TO %s FROM %f DUE TO THE LOCALE SETTINGS

Output

$ ./converter.sh 
USD-SEK Currency Convertor
Enter a USD: 10
You will get SEK 63.213800

Note that i removed the declare -i SEK from the script, since the SEK variable is NOT Integer

The harm of declare -i. This code produces:

#!/bin/bash
declare -i SEK     ;#    WOOOPS I FORGOT THE declare -i
printf "%s\n" "USD-SEK Currency Convertor"
# Get the value to convert
read -p "Enter a USD: " USD
SEK=$(echo " 6.32138 * $USD " | bc -l )
printf "You will get SEK %s\n" "$SEK"

This output:

$ ./converter.sh 
USD-SEK Currency Convertor
Enter a USD: 10
./converter.sh: line 6: 63.21380: syntax error: invalid arithmetic operator (error token is ".21380")
You will get SEK 0.000000

user000001
  • 32,226
  • 12
  • 81
  • 108
  • I tried this SEK="(1*(USD) )*6" But if I want to use decimals after 6, I don't know how to declare it? The same goes for your example, I get an syntax error. 6.32138 –  Feb 14 '13 at 16:19
  • OK thanks, So I should change to %f in printf: %f(or %F)—Display a floating-point number without exponential notation –  Feb 14 '13 at 16:34
  • Yes also you should remove the `declare -i` – user000001 Feb 14 '13 at 16:36
  • This is what I got when running the script: USD-SEK Currency Convertor Enter a USD: 10 ./test2.sh: row 6: printf: 63.21380: invalid number You will get SEK 0,000000 –  Feb 14 '13 at 16:38
  • Was that before you changed to %f? – user000001 Feb 14 '13 at 16:40
  • After, I ran your script as it is now. –  Feb 14 '13 at 16:41
  • Yes I validated it. This is caused by the `declare -i`. Could you check that you removed it? Just copy-paste the code from the answer and see if it runs in a new file. – user000001 Feb 14 '13 at 16:45
  • Added example to show what happens if you leave the declaration in. It matches your output in that case – user000001 Feb 14 '13 at 16:50
  • I ran your Complete Example and got that error code. Did a new file. –  Feb 14 '13 at 16:52
  • What I notice the difference with your output and mine is that your output is with a dot . and mine is with a comma. You will get: 63.213800 I get a , after the first zero 0,000000 –  Feb 14 '13 at 16:54
  • OK you have different locale settings. Give me a moment to see you we can resolve this – user000001 Feb 14 '13 at 16:56
  • Running 12.10 VirtualBox, with GNU bash, version 4.2.37(1)-release (i686-pc-linux-gnu). –  Feb 14 '13 at 16:59
  • what is the output of `echo " 6.32138 * 10.0 " | bc -l` ? – user000001 Feb 14 '13 at 17:01
  • If it is `63,21380` replace the `%f` in the `printf` with `%s` and you are done. – user000001 Feb 14 '13 at 17:05
  • I removed printf and ran echo as declared. So that works. What was the issue with printf? –  Feb 14 '13 at 17:08
  • It expected I float number, but in your Locale, floats have a `,` as a decimal point. Thus, the string 63.2138 was an invalid float – user000001 Feb 14 '13 at 17:10