Script math calculation

Hi Gurus,

I'm currently using HP-UX B.11.23.

I've a simple calculation script which performs the task below.

-> echo "240021344 / 1024 /1024" | bc
Output: 228

240021344 is KB value.

When I tried to perform the same calculate in Ms Excel, it produces a different result: 228.9021912.

Is it possible to perform this calculation and generate this output: 228.90 (2 decimal formats) ?

Would appreciate for any of your advice and help.

Thanks.

Regards,
Peter

Hi.

Try:

$ echo "scale=2; 240021344 / 1024 /1024" | bc
228.90

Hi scottn,

Thank you for your response.

I've tried your method and it's working on a bigger figure and having figure with smaller value as shown below:

echo "scale=2; 240021344 / 1024 /1024" | bc

Output: 228.90

echo "scale=2; 130256/ 1024 / 1024" | bc

Output: .12
-> Should be: 0.12

echo "scale=2; 1043064 / 1024 / 1024" | bc

Output: .99
-> Should be: 0.99

Are you able to advice on this issue?

Thanks a lot.

Regards,

awk '{ printf("%.2f\n", ( 130256 / 1024 / 1024) ) }'

Hi busyboy,

Thanks for your repsonse.

However, when I tried your method, it does not show anything.

awk '{ printf("%.2f\n", ( 130256 / 1024 / 1024) ) }'

Is there something wrong?

Please kindly advice.

Thanks.

Regards,
Peter

Hi, you need the BEGIN tag:

 awk 'BEGIN{ printf("%.2f\n", ( 130256 / 1024 / 1024) ) }'
1 Like

Should be:

awk 'BEGIN{printf("%.2f\n", 130256 / 1024 / 1024)}'

or:

echo "130256 1024 1024" | awk '{ printf("%.2f\n", $1 / $2 / $3)}'
1 Like

Hi Franklin52 / Klashxx,

Thanks for your help.

Had tested your method and it works perfectly. GREAT

Much appreciated.

Regards,
Peter

Without awk.

printf "%.2f\n" `echo "scale=2; 240021344 / 1024 /1024" | bc`
228.90

printf "%.2f\n" `echo "scale=2; 130256/ 1024 / 1024" | bc`
0.12

printf "%.2f\n" `echo "scale=2; 1043064 / 1024 / 1024" | bc`
0.99
1 Like
 
# printf '%.2f\n' `read val1 val2 val3 <<< "130256 1024 1024" ; echo "scale=2; $val1 / $val2 / $val3" | bc -l`
0.12

regards
ygemici

Hi methyl,

Thanks for your response.

Your method also worked like a charm. GREAT.

Regards,
Peter

---------- Post updated at 08:27 AM ---------- Previous update was at 08:25 AM ----------

Hi ygemici,

Thanks for your response.

When I tried your method, it's giving this error message.

sh: Syntax error: `<' is not expected.

Please advice. Thanks.

Regards,
Peter

---------- Post updated at 08:56 AM ---------- Previous update was at 08:36 AM ----------

Hi Gurus,

Is it possible to add "GB" at the end of this output?

For example:
228.90 GB
0.12 GB
0.99 GB

Please advice. Thanks.

Regards,
Peter

Which your shell?

# printf "%.2f%s\n" `echo "scale=2; 130256 / 1024 / 1024" | bc -l`  " GB"
0.12 GB

Hi ygemici,

I'm using SHELL=/sbin/sh (HPUX).

I've tested your method and it's working as expected.

Thank you for your help.

Regards,
Peter