Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2004
    Posts
    15

    Unanswered: Rounding numbers PLEASE

    Hello all!

    I have som numbers coming from somewhere with some strange format, such as, 19.9899992.
    Well, I really need this number with 2 decimals, 19.99.

    How do I round this number using CSHELL???

    Thx.

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    echo 'scale=2; 19.9899992 / 1' | bc

  3. #3
    Join Date
    Jun 2004
    Posts
    15
    Quote Originally Posted by vgersh99
    echo 'scale=2; 19.9899992 / 1' | bc
    Thx for your help,but I have tryed this and it does not round the number to 19.99 and yes it just cut the number like 19.98!!!

    Please I need to round it.

    Thx for your help.

  4. #4
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    if you have /usr/bin/printf:

    printf "%.2f\n" 19.9899992

    if you don't:

    echo 19.9899992 | nawk '{printf("%.2f\n", $0)}'

  5. #5
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Or if you don't have either printf or nawk but have perl, you could also use:
    Code:
    echo 19.9899992 | perl -e "$i = <>; printf \"%.2f\n\", $i"
    -PatP

  6. #6
    Join Date
    Jun 2004
    Posts
    15
    Quote Originally Posted by vgersh99
    if you have /usr/bin/printf:

    printf "%.2f\n" 19.9899992

    if you don't:

    echo 19.9899992 | nawk '{printf("%.2f\n", $0)}'

    Oh! yes, it works just fine, thx a lot man.

  7. #7
    Join Date
    Jun 2004
    Posts
    20
    Quote:
    Originally Posted by vgersh99
    echo 'scale=2; 19.9899992 / 1' | bc

    Thx for your help,but I have tryed this and it does not round the number to 19.99 and yes it just cut the number like 19.98!!!

    echo 'scale=0; 19.9899992 / 1' | bc
    try with scale =0

Posting Permissions

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