How do I get percentage output from a csh script?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ChetBritt
    New Member
    • Nov 2009
    • 1

    How do I get percentage output from a csh script?

    I am writing a csh script and trying to get percentage output figures. When I divide two figures all I get is a zero value instead of the percentage number I'm looking for. Thanks.
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    The simple answer is that you can't get percentages directly from csh. What you need is a scripting language that handles doubles or floats and most of the unix shell languages don't.

    There is a special command line calculator (bc) that will work with floating values. It takes simple string commands and spits out the value. For example:
    $ echo "40./57." | bc -l
    .70175438596491 228070
    You can fake percentages in csh by using integers instead. Instead of dividing B by A, first multiply B by 100 and divide by A. The integer answer will be the percent value. If you want to have the answer rounded to the nearest percent, try RoundUp=(B*1000 +A*5)/A*10.

    If this is too painful, try a scripting language that supports doubles. Perl is one.

    Comment

    Working...