LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-27-2021, 08:25 AM   #1
postcd
Member
 
Registered: Oct 2013
Posts: 527

Rep: Reputation: Disabled
Arithmetic operation in bash script, multiply by decimal number, how?


GNU bash, version 5.1.8(1)

How can i please do this in a bash script, i want to multiply certain number by decimal number 1.5

Non working attempts:

1)

Code:
result=$(( $number * 1.5 |bc -l ))
                    ^---------------^ SC2004: $/${} is unnecessary on arithmetic variables.
                                        ^-^ SC2079: (( )) doesn't support decimals. Use bc or awk.
                                             ^-- SC2154: bc is referenced but not assigned (for output from commands, use "$(bc ...)" ).
                                                 ^-- SC2154: l is referenced but not assigned.

Code:
2)
```
result=$(( $number * 1,5 ))
                    ^---------------^ SC2004: $/${} is unnecessary on arithmetic variables.
3)

Code:
#!/bin/bash
number=3456
result=$( echo "$number*1.5"|bc )
echo "$result"
output is decimal (5184.0) which i do not want, i want whole numbers only result
 
Old 06-27-2021, 09:16 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,813

Rep: Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958
bc does not have a built in integer function but with a scale of zero and dividing by 1 it does the same thing.
awk has a built in integer function.

Code:
#/bin/bash
number=3456

result=$(echo "$number*1.5/1" | bc )
echo "$result"

result=$( awk "BEGIN { print int($number*1.5)}" )
echo "$result"
 
1 members found this post helpful.
Old 06-27-2021, 11:11 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,797

Rep: Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002
Quote:
Originally Posted by postcd View Post
GNU bash, version 5.1.8(1)
How can i please do this in a bash script, i want to multiply certain number by decimal number 1.5 Non working attempts:
Code:
result=$(( $number * 1.5 |bc -l ))
                    ^---------------^ SC2004: $/${} is unnecessary on arithmetic variables.
                                        ^-^ SC2079: (( )) doesn't support decimals. Use bc or awk.
                                             ^-- SC2154: bc is referenced but not assigned (for output from commands, use "$(bc ...)" ).
                                                 ^-- SC2154: l is referenced but not assigned.
Code:
2)
result=$(( $number * 1,5 ))
                    ^---------------^ SC2004: $/${} is unnecessary on arithmetic variables.
Code:
#!/bin/bash
number=3456
result=$( echo "$number*1.5"|bc )
echo "$result"
output is decimal (5184.0) which i do not want, i want whole numbers only result
As asked many times in the past, do basic research first. From the bash documentation (bold/emphasis theirs):
Quote:
Originally Posted by Bash Documentation
Section "6.5 Shell Arithmetic":

The shell allows arithmetic expressions to be evaluated, as one of the shell expansions or by using the (( compound command, the let builtin, or the -i option to the declare builtin.

Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error
So you cannot using bash alone. Doing basic research for other ways to script this yields a lot of suggestions and examples. The simplest being:
Code:
#!/bin/bash
declare -i number=3456
result=$( echo "$number*1.5/1"|bc )
echo "$result"
You did not declare the initial variable as an integer, and doing a simple divide by one gives you what you're after. Along with many other possibilities, all using standard Linux utilities:
Code:
calc 3456*1.5
bc <<< "scale=0; (3456*1.5/1)"
perl -E "say 3456*1.5"
python -c "print(int(3456*1.5))
lua -e "print(string.format('%.0f',3456*1.5))"
...along with THOUSANDS of other ways to script/write this, with easily found examples.
 
Old 06-27-2021, 01:15 PM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,688

Rep: Reputation: Disabled
Well, the number 1.5 lends itself particularly well to doing this in Bash:
Code:
#!/bin/bash
number=3456
result=$((number*3/2))
echo "$result"
Or even
Code:
#!/bin/sh
number=3456
result=$((number+number/2))
echo $result

Last edited by shruggy; 06-27-2021 at 02:18 PM.
 
1 members found this post helpful.
Old 06-27-2021, 01:39 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,797

Rep: Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002
Quote:
Originally Posted by shruggy View Post
Well, the number 1.5 lends itself particularly well to doing this in Bash:
Code:
#!/bin/bash
number=3456
result=$((number*3/2))
echo "$result"
Nice..always more than one way to do it.
 
Old 06-28-2021, 04:21 PM   #6
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,157
Blog Entries: 6

Rep: Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836
Quote:
How can i please do this in a bash script, i want to multiply certain number by decimal number 1.5
Couple of quick examples:

#Multiply by 1.5
Code:
for n in {1..20}; do
    if (($n >= 1 && $n <= 6)); then
        echo -e "\nProducts for ("$n" * 1.5) is:"
        ans=$(("$n"00*3/2))
        echo "("$n" * 1.5) = $((${ans:0: 1}+0)).${ans: 1}"
    elif (($n >= 7 && $n <= 20)); then
        echo -e "\nProduct for ("$n" * 1.5) is:"
        ans=$(("$n"00*3/2))
        echo "("$n" * 1.5) = $((${ans:0: 2}+0)).${ans: 2}"
    fi
done

#Divide by 1.5
Code:
for n in {1..30}; do
    echo -e "\nQuotient for ("$n" / 1.5) is:"
    ans=$(("$n"00*2/3))
    echo "("$n" / 1.5) = $((${ans:0: -2}+0)).${ans: -2}"
done

Last edited by teckk; 06-28-2021 at 04:24 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
4gig file trying to replace decimal number A with decimal number B tasdca Linux - Software 5 03-26-2015 05:51 PM
[SOLVED] how to multiply an integer with a decimal using shell scripting? ivahbee Linux - General 2 10-30-2014 03:38 AM
[SOLVED] BC provides incorrect decimal results, but only on the last two decimal places... standard_output Linux - Newbie 4 06-27-2012 05:30 PM
[SOLVED] arithmetic operation in bash xeon123 Linux - Newbie 6 03-10-2011 07:12 PM
How to make arithmetic operation of data file with script file? mauludi Linux - Newbie 5 09-01-2010 05:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration