INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

divide in bash to 1 decimal place

divide in bash to 1 decimal place

(OP)
hello,

on a system having no bc, perl, awk, and to which I have no administrator rights (only common user account), with restricted shell (bash), sed and expr available, I need to be able divide numbers to 1 decimal place:

> echo $((10/10))
1
> echo $((5/10))
0

I'd like to get 1.0, 0.1, etc (so leading zero for <1)

also builtin let gives 0
> let "m=1/4";echo $m
0

thanks in advance for a hint!

RE: divide in bash to 1 decimal place

bash doesn't have floating point maths built in, so without 'bc' and it's associated scale=n, decimal places simply are not going to happen.

try expr 4 / 3 and see what the result is.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: divide in bash to 1 decimal place

(OP)

so it looks there is no local solution on this host...

> expr 4 / 3
1

> expr 1 / 3
0

RE: divide in bash to 1 decimal place

Hi

If you want to keep your script pure Bash, you can do a trick by multiplying the dividend enough so integer arithmetics give precise enough result, then add separate the fractional part with string operation :

CODE --> Bash

m=$(( 4 * 10 / 3 ))
echo "${m:0:-1}.${m: -1}" 

But better use tool instead. Either the already mentioned Bc with its verbose syntax, or Dc :

CODE --> command line

master # bc <<< 'scale=1;4/3'
1.3

master # dc <<< '1k4 3/p'
1.3 

Feherke.
feherke.ga

RE: divide in bash to 1 decimal place

If it's for scale 1 why not simply multiply the numerator by 10 and use string manipuation to insert the point.
Maybe something like this (rough draft):

CODE

#!/bin/bash

X=3
Y=4

X10=$(($X*10))

R=$(($X10/$Y))
[ $(($X/$Y)) -ge 1 ] && RD=${R:0:${#R}-1}.${R:${#R}-1} || RD=0.$R

echo $RD 
Just keep in mind that your result is a string...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close