Linked Questions
65 questions linked to/from How to do integer & float calculations, in bash or other languages/frameworks?
11
votes
4
answers
82k
views
bash + how to calculate percentage from number [duplicate]
how to calculate percentage from number
for example we set
number=248
and we want to know what is the 80% from $number
so how to calculate it in bash ?
expected output 198 ( exactly is 198.4 but ...
14
votes
2
answers
23k
views
Can bash do floating-point arithmetic without using an external command? [duplicate]
I have read that bash can do integer arithmetic without using an external command, for example:
echo "$((3 * (2 + 1)))"
Can bash also do floating-point arithmetic without using an external command?
5
votes
4
answers
23k
views
Perform floating point arithmetic in shell script variable definitions [duplicate]
I understand bash and some other interpreters only perform arithmetic for integers. In the following for loop, how can I accomplish this? I've read that bc can be used but am not sure how to use bc ...
9
votes
1
answer
41k
views
Is it possible to get a decimal output from doing division in Bash? [duplicate]
Basically in Bash, what I'm trying to do is take a input of seconds from a user and find the hours. So basically if the user enter 35 the output should be 0.00972222.
But bash gives me is a zero.
...
3
votes
1
answer
17k
views
How can subtract 2 floats which have been extracted from 2 other files with BASH [duplicate]
I'm trying to get the difference between two numbers which I have taken from two files. I think my code will make sense:
I've tried to make it work by two different methods, didn't work. What I get ...
12
votes
1
answer
8k
views
bash + how to calculate very long numbers in bash? [duplicate]
How to calculate very long numbers in bash?
param=$(( 3247238523785623478565 + 53453453252345346534563412634 ))
echo $param
3420247196502465471
as we see here, this isn't the right answer because of ...
1
vote
4
answers
10k
views
Floating point numbers in bash [duplicate]
Let's say I have a variable and I want to print 5 significant digits after I multiplied it by 1000. zsh can do it:
zsh$ x=2.8026407e+00
zsh$ printf "%.5g\n" "$(( 1000*${x} ))"
zsh> 2802.6
Can bash ...
3
votes
1
answer
30k
views
Divide a variable by a number? [duplicate]
Basically I need to convert centimetres to inches which I am trying to do by diving the area in centimetres by 2.54.
But I just cannot get this to work.
echo "please enter width and then height"
...
4
votes
1
answer
22k
views
Trying to multiply a float in bash not working [duplicate]
I have this script that is to rescale images to a percentage value
#!/bin/bash
percent=$1
echo $percent
for img in `find *.png`;
do
echo Processing file $img
width=$( mdls $img | grep ...
3
votes
4
answers
4k
views
How to sum variables from file in Bash [duplicate]
I need to sum numbers from a file line by line.
The file:
1.0
0.46
0.67
I want to sum, then divide 3.
I currently have:
while IFS= read -r var
do
x=$(($var + $x)) | bc -l
done < "file.txt"
...
0
votes
2
answers
8k
views
Re-assigning (specifically, incrementing) a variable in a bash script [duplicate]
Possible Duplicate:
How can I do command line integer & float calculations, in bash, or any language available?
I would like to simply re-assign a variable (to be specific, increment it by 0....
4
votes
1
answer
7k
views
float Point math in Unix shell script and assign it to variable [duplicate]
I have been doing integer calculation like this.
a=12
b=23
c=$((a-b))
echo $c
But, now for float numbers its is failing
i read that we can do that using bc however i want to assign the result in ...
0
votes
2
answers
2k
views
How to divide numbers and return as float in #!/bin/sh [duplicate]
Hi I have to divide two numbers in #!/bin/sh
$x = 5678
$x/1000
but calculations return only integer: 5.
I need 5,678.
I've tried
$x/1000.0
$x/1000.0 | bc
on first method expect integer
second ...
1
vote
1
answer
3k
views
Shell scripting to print multiples of 5 including 1 and 100 [duplicate]
#!/bin/bash
x=1
while [ "$x" -lt 20 ]
do
echo "$x"
x=("$x" * 5);
done
Getting error as syntax error near unexpected token `done'
and getting output as 11111111111....
5
votes
1
answer
1k
views
Bash shell decimal test [duplicate]
My shell is bash and I have three variables
x=5; y=7; z=7.5
I am trying to use the below statement to test the equality of variables $y and $z as shown below
Command: test $z -eq $y; echo $?
But, it ...