Results 1 to 9 of 9

Thread: how to write a script that can multipy decimal numbers

  1. #1
    Join Date
    Sep 2013
    Beans
    18

    how to write a script that can multipy decimal numbers

    Hi

    I try to figure out and spent 6 hours on the net looking for help on how can i write simple script that can multiply decimal numbers with no luck, here is an example

    Code:
    a=2 
    b=2.2
    answer= `expr $a \* $b`
    echo "$a * $b = $answer"
    when i run the script and there was an error "expr: syntax error * 2.2 = "




    Thanks

  2. #2
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: how to write a script that can multipy decimal numbers

    You can use bc

    example

    Code:
     echo "scale=3; 0.8*0.7"|bc

  3. #3
    Join Date
    Sep 2013
    Beans
    18

    Re: how to write a script that can multipy decimal numbers

    thanks for the quick reply, may i ask you a question is it correct if a do like this

    a=2
    b=2.2
    echo "scale=3; $a*$b"|bc

  4. #4
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: how to write a script that can multipy decimal numbers

    Try it out for yourself!

    Seriously, though, that works fine, and you can also skip the echo and pipe by redirecting the string right into bc:

    Code:
    a=2
    b=2.2
    bc <<< "scale=3; $a*$b"
    4.4

  5. #5
    Join Date
    Dec 2007
    Beans
    8,968
    Distro
    Lubuntu 16.04 Xenial Xerus

    Re: how to write a script that can multipy decimal numbers

    Useful thread! I learned about bc and <<<
    Shortened urls: See point #11 here. And clicking on them could be risky.
    Use ubuntu-support-status with caution.

  6. #6
    Join Date
    Sep 2013
    Beans
    18

    Re: how to write a script that can multipy decimal numbers

    Thank you very much

  7. #7
    Join Date
    Sep 2013
    Beans
    18

    Re: how to write a script that can multipy decimal numbers

    Thanks guys for the help

  8. #8
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: how to write a script that can multipy decimal numbers

    You are welcome

    Please click on Thread Tools at the top of this page and mark the thread as SOLVED. It will help other people searching for a solution of similar problems.

  9. #9
    Join Date
    Apr 2008
    Location
    Southern California, USA
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: how to write a script that can multipy decimal numbers

    You learn something everyday. I've used bash for years. First time I came across "<<<" usage. A Here String indicator, as opposed to a Here Document "<<". Interesting
    Here Strings. A variant of here documents, the format is: <<<word
    Just marking for further reference. Thanks for bringing up this topic and answers.
    Last edited by VMC; October 28th, 2013 at 04:01 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •