Find the answer to your Linux question:
Results 1 to 5 of 5
Hello all- I've created a crazy script that I am so proud of except I can't figure out a simple math problem that is holding up the project. I have ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    15

    Simple Math Question

    Hello all-

    I've created a crazy script that I am so proud of except I can't figure out a simple math problem that is holding up the project.

    I have a line in my script that produces a number with a decimal. In my case the number is 75.5. I can't get rid of the ".5". How do I round a number up or down?

    It shouldn't be a big deal but my script errors out because the shell can't handle numbers with a ".".

    How do I convert 75.5 to 75?

    The line from my script is:

    fsize=$((size / 1000261785))

    and $fsize almost always has a decimal.

    Any help would be greatly appreciated. Thanks in advance

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    That line from your script can't be accurate.

    Wasn't there supposed to be a $ before the "size" in that line?

    And just to make it easier for us to help you, can you post a tiny, complete script which misbehaves? Something which we can copy and paste into a window and experiment with for you?

    Just a suggestion.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Dec 2007
    Posts
    5
    Im not familiar with command-line scripting, but if there is a Math.floor() or Math.ceiling() function this should solve your problem. Or perhaps Math.round.

    Or convert it to a string and get a substring from the beginning to the first instance of a decimal.



    Quote Originally Posted by rkasowan View Post
    Hello all-

    I've created a crazy script that I am so proud of except I can't figure out a simple math problem that is holding up the project.

    I have a line in my script that produces a number with a decimal. In my case the number is 75.5. I can't get rid of the ".5". How do I round a number up or down?

    It shouldn't be a big deal but my script errors out because the shell can't handle numbers with a ".".

    How do I convert 75.5 to 75?

    The line from my script is:

    fsize=$((size / 1000261785))

    and $fsize almost always has a decimal.

    Any help would be greatly appreciated. Thanks in advance

  4. #4
    Just Joined!
    Join Date
    May 2007
    Posts
    15
    I found a simple solution that met my needs:

    Code:
    #!/bin/tcsh -f
    read v
    echo $v/1 | bc
    exit 0
    Thanks for the input - I see a few new commands I need to check out.

    -= robbie =-

  5. #5
    drl
    drl is online now
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    We first note:
    ARITHMETIC EVALUATION
    The shell allows arithmetic expressions to be evaluated, under certain
    circumstances (see the let builtin command and Arithmetic Expansion).
    Evaluation is done in fixed-width integers ...

    ** exponentiation
    * / % multiplication, division, remainder
    + - addition, subtraction

    ...
    Shell variables are allowed as operands; parameter expansion is per-
    formed before the expression is evaluated. Within an expression, shell
    variables may also be referenced by name without using the parameter
    expansion syntax.

    -- excerpts from man bash under ARITHMETIC EVALUATION

    and:

    let: let arg [arg ...]
    Each ARG is an arithmetic expression to be evaluated. Evaluation
    is done in fixed-width integers with no check for overflow
    As an example:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1       Demonstrate arithmetic in shell.
    
    set -o nounset
    echo
    
    debug=":"
    debug="echo"
    
    ## Use local command version for the commands in this demonstration.
    
    echo "(Versions displayed with local utility \"version\")"
    version >/dev/null 2>&1 && version bash
    
    echo
    
    x=7
    echo " x is $x"
    
    echo
    echo " x/3 is $(( x/3 ))"
    
    echo
    echo " x / 3 is $(( x / 3 ))"
    
    echo
    echo " using \"let\":"
    let y=x/2
    echo " y is $y"
    
    exit 0
    Producing:
    Code:
    % ./s1
    
    (Versions displayed with local utility "version")
    GNU bash 2.05b.0
    
     x is 7
    
     x/3 is 2
    
     x / 3 is 2
    
     using "let":
     y is 3
    See also the very valuable collection of explanations and examples in http://www.tldp.org/LDP/abs/html/arithexp.html , the Advanced Bash-Scripting Guide.

    The sample above shows no decimal fractions, only truncation, as expected according to man bash.

    As Bill suggested, if you are having problems -- such as getting decimals, as in 75.5 -- then please post the smallest script in its entirety that illustrates the problem.

    Best wishes ... cheers, drl

    ---

    Standard advice: Do not use csh family for scripting.
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...