Unix/Linux Math Decimal to Whole Number Format?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    The problem? I hope I fill this out correctly. I have a program that runs like a cash register. It works and functions correctly. The question is how can I make it "Not" use decimal's and use whole numbers?

  2. Relevant commands, code, scripts, algorithms:
    The code:

12 clear
13 total=0
14 grandtotal=0
15 taxtotal=0
16 most=0
17 least=0
18 counter=0
19 echo "Enter a price:"
echo "Press Enter twice when you are done to see the total."

20 echo
21 echo -n "Price: $" ; read a
22 echo
23 most=$a
24 least=$a
25 total=$a
26 while test $a
27 do
28 echo -n "Please enter another price: $"
29 read a

30 echo
31 let counter=$counter+1
32 if [[ $a = "" ]]
33 then break
34 else
35 (( total += a))
36 if [[ $a -lt $least ]]
37 then least=$a
38 fi
39 if [[ $a -gt $most ]]

40 then most=$a
41 fi
42 fi
43 done
44 clear
45 echo -n "The total is: $"
46 echo $total
47 echo
48 if [ $total -gt 1000 ]
49 then tax=.03

50 else
51 tax=.06
52 fi
53 taxtotal="$tax*$total"
54 echo -n "The Tax is: $"
55 echo $taxtotal | bc
56 echo
57 echo -n "The Grand Total is: $"
58 grandtotal=$total+$taxtotal
59 echo $grandtotal | bc

50 echo
51 echo -n "The total number of items is: "
52 echo $counter
53 echo
54 echo -n "The most expensive item was: $"
55 echo $most
56 echo
57 echo -n "The lowest item was: $"
58 echo $least
59 echo

  1. The attempts at a solution (include all code and scripts):

I have tried changing lines 49 and 51 to
49 then tax=$total=3100
49 then tax="$total=3
100"
49 then tax="$total/3*100"

51 tax=$total=6100
51 tax="$total=6
100"
51 tax="$total/6*100"

This has been a self taught class for the most part, the stuff we are asked to do is way beyond what INTRO is suppose to be about in a 9 week class.

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Baker College, Allen Park, Michigan, United States, Mahmoud Zali, Intro to Linux/Unix LUX205 (no link on campus)

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

do you want to truncate or round to remove the decimal?

all variables in shell scripting will be treated as integers for calculations and will be truncated. if you do something like tax=.03 this is really assigning the string ".03" to the variable $tax.

1 Like

Please (in order easy to copy and try your script):

  1. Use code tags for code.
  2. Remove line numbers.

And what is wrong with just changing 0.03 to 3 and 0.06 to 6?
---
Ok, a silly question.

Frank, that is good question. I don't wanna sound foolish, but I honestly don't know what truncated means. I would say round up to remove the decimal. Again, this was suppose to be an "Intro" class, teach us the basic's, but our teacher is well, less than active.

If anything I'm just going to turn it in as is and take the deductions on the decimal points. This Unix stuff is not for me, I have no plans on pursuing this as a career.

Just curious -- in this context why would you convert .03 to 3 or .06 to 6 for any reason? I cannot think of any.

---------- Post updated at 22:08 ---------- Previous update was at 22:05 ----------

example truncation

1.2 converted to an integer equals 1. everything after the decimal is truncated.

IMO I would avoid floating point math in shell scripting. use bc or another language like Perl/awk to do the work.

Yazu, sorry for line numbers, as far as code tags, I thought it was code tagged. This is my very first post, ever here for coding questions.

Im not an expert or even a novice, I'm a new beginner who didn't even want to take this class in the first place. So sorry If I make mistakes.

But anyway, what happens when I just use a 3 or a 6 is that when I type in lets say 2 for the first price and 2 for the second price when the calculation comes back it tells me the tax is $24 and my grand total is $28 dollars.
With the decimal's in place its .24 and the total 4.24

---------- Post updated at 11:12 PM ---------- Previous update was at 11:08 PM ----------

I know Frank, I don't know either, the program is fine, I have no clue on why this teacher wants us to use whole numbers in place of .03 for the tax. Like I mentioned I'm most likely going to just hand it in as is. I was just curious if you could do it as whole numbers.

Thank you kindly for your reply's. I'll find the thank button and click it. Because I am grateful.

Probably because most shells can't do math on numbers with decimal points. They'd either give a completely unexpected result, or discard the decimal points entirely.

Another reason whole numbers are used for finances is accuracy. Most languages which support decimal points use floating point numbers to do so, which actually have a fixed number of decimal places and multiply by 2^n to represent very large or very small numbers. This is a good approximation for math, but if you're dealing with finances, you might create headaches for accountants by being out quite a few cents here and there every time you add huge numbers of dollars.

How to use whole numbers instead of decimal points? Calculate everything in cents. No decimals needed (except maybe for printing).

How I'd convert decimal point to whole numbers is to multiply by 100... since you can't do that math on decimal-point numbers in the shell, use a number trick instead; split "13.42" into "13" and "42", make sure "42" is exactly two digits long, then stick them together into "1342". Voila, whole number in cents.

How to do this depends on your shell. What is it?