LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-02-2010, 12:36 PM   #1
gary_in_springhill
Member
 
Registered: Mar 2008
Posts: 136

Rep: Reputation: 21
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
 
Old 02-02-2010, 03:31 PM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by gary_in_springhill View Post
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"
 
Old 02-02-2010, 04:06 PM   #3
gary_in_springhill
Member
 
Registered: Mar 2008
Posts: 136

Original Poster
Rep: Reputation: 21
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??
 
Old 02-02-2010, 05:55 PM   #4
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by gary_in_springhill View Post
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.
 
1 members found this post helpful.
Old 02-02-2010, 08:18 PM   #5
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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 ""
 
1 members found this post helpful.
Old 02-03-2010, 01:34 AM   #6
gary_in_springhill
Member
 
Registered: Mar 2008
Posts: 136

Original Poster
Rep: Reputation: 21
got it

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH printf and ftp remirez83 Programming 5 01-13-2010 12:56 PM
BASH - printf printing escape codes even though I have %0b defined DevonB Linux - Newbie 5 12-23-2009 01:42 PM
Rounding in pure bash anonguy9 LinuxQuestions.org Member Success Stories 4 03-29-2009 12:20 PM
LXer: The trouble with rounding floating point numbers LXer Syndicated Linux News 0 08-12-2006 01:54 PM
bash printf anyone? gumby Programming 4 05-13-2003 11:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:09 AM.

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