Complete Split Number in Bash

How would I round the result from two separated numbers, for example.

3/2 

As I,

 testOne=$((3/2)) 

$ testOne contains "1" when it should round to "2" as an answer from 3/2 = 1.5

+33
math linux bash shell rounding
Mar 07 '10 at 5:25
source share
9 answers

To round off in truncated arithmetic, simply add (denom-1) to the numerator.

Example: rounding:

 N/2 M/5 K/16 

Example: rounding:

 (N+1)/2 (M+4)/5 (K+15)/16 

To make a round to the nearest one, add (denom/2) to the numerator (half will round):

 (N+1)/2 (M+2)/5 (K+8)/16 
+53
Mar 07 '10 at 5:27
source share

bash will not give you the correct 3/2 result, since it does not perform floating math. you can use tools like awk

 $ awk 'BEGIN { rounded = sprintf("%.0f", 3/2); print rounded }' 2 

or bc

 $ printf "%.0f" $(echo "scale=2;3/2" | bc) 2 
+21
Mar 07 '10 at 8:14
source share

A good solution is to get the closest round number.

 var=2.5 echo $var | awk '{print int($1+0.5)}' 

The logic is simple, if the decimal value of var is less than .5, then the closest value will be an integer. Well, if the decimal value is greater than .5, then the next integer value is added, and since awk only accepts the integer part. The problem is resolved.

+17
Jun 12 '13 at 6:50
source share

If you have an integer division of positive numbers that is rounded to zero, then you can add one less divisor to the dividend to round it.

That is, replace X / Y with (X + Y - 1) / Y

Evidence:

  • Case 1: X = k * Y (X is an integer multiple of Y): In this case, we have (k * Y + Y - 1) / Y , which is divided into (k * Y) / Y + (Y - 1) / Y . The party (Y - 1)/Y rounded to zero, and we are left with a coefficient k . This is exactly what we want: when the inputs are divided, we want the adjusted calculation to still produce the correct accurate coefficient.

  • Case 2: X = k * Y + m where 0 < m < Y (X is not a multiple of Y). In this case, we have the numerator k * Y + m + Y - 1 , or k * Y + Y + m - 1 , and we can write the division as (k * Y)/Y + Y/Y + (m - 1)/Y Since 0 < m < Y , 0 <= m - 1 < Y - 1 , therefore, the last term (m - 1)/Y vanishes. Remains (k * Y)/Y + Y/Y , which work up to k + 1 . This shows that the behavior is rounded. If we have X , which is k multiple of Y , if you add only 1 to it, the division is rounded to k + 1 .

But this rounding is extremely opposite; all inaccurate divisions go from scratch. How about something in between?

This can be achieved by β€œfilling” the numerator with Y/2 . Instead of X/Y calculate (X+Y/2)/Y Instead of evidence, release the empirical on this:

 $ round() > { > echo $((($1 + $2/2) / $2)) > } $ round 4 10 0 $ round 5 10 1 $ round 6 10 1 $ round 9 10 1 $ round 10 10 1 $ round 14 10 1 $ round 15 10 2 

Whenever a divisor is an even, positive number, if the numerator is comparable to half that number, it is rounded and rounded if it is less than that.

For example, round 6 12 goes to 1 , like all values ​​equal to 6 , modulo 12 , such as 18 (which corresponds to 2), etc. round 5 12 goes to 0 .

For odd numbers, the behavior is correct. None of the exact rational numbers are halfway between two consecutive multiple. For example, with denominator 11 we have 5/11 < 5.5/11 (exact middle) < 6/11 ; and round 5 11 rounded, and round 6 11 rounded.

+6
Jun 16 '14 at 22:43
source share

Given a floating point value, we can trivially round it with printf:

 # round $1 to $2 decimal places round() { printf "%.$2f" "$1" } 

Then

 # do some math, bc style math() { echo "$*" | bc -l } $ echo "Pi, to five decimal places, is $(round $(math "4*a(1)") 5)" Pi, to five decimal places, is 3.14159 

Or, to use the original request:

 $ echo "3/2, rounded to the nearest integer, is $(round $(math "3/2") 0)" 3/2, rounded to the nearest integer, is 2 
+4
Jun 14 '15 at 23:22
source share

Another solution is division inside the python command. For example:

 $ numerator=90 $ denominator=7 $ python -c "print (round(${numerator}.0 / ${denominator}.0))" 

It seems less archaic to me than using awk.

+2
May 04 '12 at 3:20 p.m.
source share

For rounding you can use the module.

The second part of the equation will add True if there is a remainder. (True = 1; False = 0)

ex: 3/2

 answer=$(((3 / 2) + (3 % 2 > 0))) echo $answer 2 

ex: 100/2

 answer=$(((100 / 2) + (100 % 2 > 0))) echo $answer 50 

ex: 100/3

 answer=$(((100 / 3) + (100 % 3 > 0))) echo $answer 34 
+2
Sep 19 '14 at 22:43
source share

I think that should be enough.

 $ echo "3/2" | bc 
+1
Jun 19 '13 at 19:46
source share

If the decimal separator is a comma (for example: LC_NUMERIC = fr_FR.UTF-8, see here ):

 $ printf "%.0f" $(echo "scale=2;3/2" | bc) bash: printf: 1.50: nombre non valable 0 

Substitution is necessary for ghostdog74 solution:

 $ printf "%.0f" $(echo "scale=2;3/2" | bc | sed 's/[.]/,/') 2 

or

 $ printf "%.0f" $(echo "scale=2;3/2" | bc | tr '.' ',') 2 
+1
Aug 19 '14 at 18:04
source share



All Articles