Bash script formatting problem
I've created a simple loan script and I am trying to display a list of information on the screen: PaymentNumber, Interest, MonthlyPayment and Balance. When I'm calculating some of these values the decimals number exceed 2 digits past the decimal point. How can I go about formatting so it doesn't happen? I also want everything to be displaybed right justified.
Here is some of my code: READ PRICE READ YRLY_INTEREST READ MONTHLY_PYMT MONTHLY_INTEREST=$(echo "scale=3; $YRLY_INTEREST / 12"|bc) PAYMENT_NO=0 BALANCE=$PRICE while [ `echo $BALANCE | cut -d. -f1` -gt 0 ] do PAYMENT_NO=`expr $PAYMENT_NO + 1` INTEREST=$(echo "scale=3; $MONTHLY_INTEREST / 100* $BALANCE"|bc) BALANCE=$(echo "scale=2; $BALANCE + INTEREST - $MONTHLY_PYMT"|bc) if [ `echo $BALANCE | cut -d. -f1` -lt 0 ] then MONTHLY_PYMT=$(echo "scale=2; $MONTHLY_PYMT + $BALANCE"|bc) BALANCE=0 fi printf "%9s %9s %9s %9s\n" $PAYMENT_NO $INTEREST $MONTHLY_PYMT $BALANCE done I've tried other things arguments in the printf statement but won't give me the results wanted. I want everything right justified and if a number is a decimal i want it to be two places past the decimal point. Thanks! |
bash printf formatting, string number conversion
There were a few more little things amiss in the code; you'll have to look carefully at this to see what's changed. It works, but there are better ways of doing some things in here - I've left it mostly as it was to make it (hopefully) easier to spot the differences..
Code:
#!/bin/bash Code:
499 |
Not all functions
Hi New2LinuxMN
I've never used bc before so it's been nice to play with it. Reading the manual it says that not all functions take any notice of scale. Compare the output of Quote:
Quote:
Quote:
Quote:
|
I suggest you use awk (or a language such as Python) to do this
Code:
awk 'BEGIN{ Code:
# ./shell.sh |
Well thanks everyone for your help with this.
By question for GrapefuiTgirl would be how could I get the numbers to be right justified.? |
Quote:
Putting the "9" in the right spot produces this: Code:
sasha@reactor: sh script |
Thank you!
|
All times are GMT -5. The time now is 02:03 PM. |