Find the answer to your Linux question:
Results 1 to 2 of 2
This is puzzling me. $ x=165 $ printf "%2.1f\n" $[$x/10] 16.0 The result is 16.0 instead of 16.5. If I put $ printf "%2.1f\n" $[$x/10.0] bash: 165/10.0: syntax error: invalid ...
  1. #1
    Just Joined!
    Join Date
    May 2011
    Posts
    2

    Need help with printf in bash shell

    This is puzzling me.

    $ x=165
    $ printf "%2.1f\n" $[$x/10]
    16.0

    The result is 16.0 instead of 16.5.

    If I put

    $ printf "%2.1f\n" $[$x/10.0]
    bash: 165/10.0: syntax error: invalid arithmetic operator (error token is ".0")

    So what should I do to get the correct result, which is 16.5?

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    welcome to the forum

    because '/' truncates the quotient, you need to
    pick up the remainder: like this

    printf "%2.1f\n" $[$x/10].$[$x%10]
    the sun is new every day (heraclitus)

Posting Permissions

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