LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash rounding up numbers with printf (https://www.linuxquestions.org/questions/programming-9/bash-rounding-up-numbers-with-printf-786485/)

gary_in_springhill 02-02-2010 12:36 PM

Bash rounding up numbers with printf
 
For eg: $NUMBEROFPASSES=6.19

to round up I use

NUMBEROFPASSES=$(printf %.0f $NUMBEROFPASSES)

What I need to do is round up from eg: 6.10 to be 7 and if lower than 6.10 round down to 6

Any help would be appreciated
Thanks
Gary

David1357 02-02-2010 03:31 PM

Quote:

Originally Posted by gary_in_springhill (Post 3849817)
What I need to do is round up from eg: 6.10 to be 7 and if lower than 6.10 round down to 6

From the man page for bash:
Code:

ARITHMETIC EVALUATION
      The  shell allows arithmetic expressions to be evaluated, under certain
      circumstances (see the let and declare builtin commands and  Arithmetic
      Expansion).  Evaluation  is done in fixed-width integers with no check
      for overflow, though division by 0 is trapped and flagged as an  error.

If you need floating point, you'll have to add some awk magic:
Code:

#!/bin/bash
NUMBER=6.19
NUMBER=$(echo $NUMBER | awk '{ print $0 + .90 }')
NUMBER=$(printf "%0.f" $NUMBER)
echo "NUMBER = $NUMBER"


gary_in_springhill 02-02-2010 04:06 PM

very useful
 
Thanks for your reply it put me on the right track for sure, but I tested it with various numbers eg: 4.99 and it rounded to six??

David1357 02-02-2010 05:55 PM

Quote:

Originally Posted by gary_in_springhill (Post 3850030)
Thanks for your reply it put me on the right track for sure, but I tested it with various numbers eg: 4.99 and it rounded to six??

This survives all of your test cases
Code:

#!/bin/bash

float()
{
        # Convert the first parameter to a floating point number
        # no matter how broken the input
        VALUE=$(printf "%f" $1)
        echo $VALUE
}

greater()
{
        VALUE=$(float $1)
        CHECK=$(echo $VALUE | awk '{ result = ( $1 > 0.0 ) ; print !result }')
        return $CHECK
}

offset()
{
        VALUE=$1
        if greater $VALUE ; then
                # Adjust the rounding offset
                RESULT=$(echo $VALUE | awk '{ print 0.5 - $1 }')
        else
                # Use the standard rounding offset
                RESULT="0.0"
        fi
        echo $RESULT
}

round ()
{
        NUMBER=$1
        OFFSET=$2
        # If the offset is greater than zero
        if greater $OFFSET ; then
                # Add the non-standard rounding offset to the number
                RESULT=$(echo $NUMBER $OFFSET | awk '{ print $1 + $2 }')
        else
                # Add the standard rounding offset to the number
                RESULT=$(echo $NUMBER | awk '{ print $1 + .50 }')
        fi
        echo "$NUMBER, $OFFSET -> $(printf "%0.f" $RESULT)"
}

NUMBER=6.19
OFFSET=$(offset .10)
round $NUMBER $OFFSET
NUMBER=6.09
OFFSET=$(offset .10)
round $NUMBER $OFFSET
NUMBER=4.99
OFFSET=$(offset .00)
round $NUMBER $OFFSET

The script can probably be simplified.

crabboy 02-02-2010 08:18 PM

how about this:
Code:

#!/bin/sh

NUM=$1

bc << EOF
num = $NUM;
base = num / 1;
if (((num - base) * 10) > 1 )
    base += 1;
print base;
EOF
echo ""


gary_in_springhill 02-03-2010 01:34 AM

got it
 
Thanks to both of you , this did the trick .
Thanks again


All times are GMT -5. The time now is 05:11 PM.