Unix/Linux Math Decimal to Whole Number Format?

Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Unix/Linux Math Decimal to Whole Number Format?
# 1  
Old 08-23-2011
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

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

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

51 tax=$total=6*100
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.

4. 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).
# 2  
Old 08-23-2011
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.
This User Gave Thanks to frank_rizzo For This Post:
# 3  
Old 08-23-2011
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.

Last edited by yazu; 08-24-2011 at 12:21 AM.. Reason: silly question
# 4  
Old 08-24-2011
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.
# 5  
Old 08-24-2011
Originally Posted by yazu
And what is wrong with just changing 0.03 to 3 and 0.06 to 6?
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 ----------

Originally Posted by Iceman69
Frank, that is good question. I don't wanna sound foolish, but I honestly don't know what truncated means.
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.
# 6  
Old 08-24-2011
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.
# 7  
Old 08-24-2011
Originally Posted by Iceman69
I have no clue on why this teacher wants us to use whole numbers in place of .03 for the tax.
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to control the decimal points for p-values in scientific format?

Dear all, I have a txt file with only one column which contains p values. My data looks like this: 5.04726976606584e-190 2.94065711152402e-189 2.94065711152402e-189 9.19932135717279e-176 1.09472516659859e-170 1.24974648916809e-170 0.1223974648916 0.9874974648916 ... what I want... (2 Replies)
Discussion started by: forevertl
2 Replies

2. UNIX for Dummies Questions & Answers

Linux Math Help

I am struggling with scripting this challenge a friend and I have. You have file1 and its contents is a single number you have file 2 and its contents are a different number you want to add file1 to file2 and have the output be put into file3 (3 Replies)
Discussion started by: minkyboodle
3 Replies

3. Shell Programming and Scripting

Sort Decimal number in UNIX

Hello Everyone, In one of my script, I would like to sort the decimal numbers. For e.g. If I have numbers like 1.0 1.1 1.2 2.0 2.1 3.0 4.0 5.0 6.0 7.0 7.1 7.10 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 I would like to sort them 1.0 1.1 1.2 2.0 2.1 3.0 4.0 5.0 6.0 7.0 7.1 7.2 7.3 7.4... (3 Replies)
Discussion started by: sachinrastogi
3 Replies

4. Shell Programming and Scripting

Awk - Summation in Proper decimal Format

Hi I am executing below command to do summation on 46th coloumn. cat File1| awk -F"|" '{p += $46} END { printf"Column Name | SUM | " p}' I am getting output as Column Name | SUM | 1.01139e+10 Here I want output in Proper decimal format. Can someone tell me what change is required for same? (1 Reply)
Discussion started by: sanranad
1 Replies

5. Shell Programming and Scripting

number of digits after decimal

Hi All, I have a file of decimal numbers, cat file1.txt 1.1382666907 1.2603107334 1.6118799297 24.4995857056 494.7632588468 560.7633734425 ..... I want to see the output as only 7 digits after decimal (5 Replies)
Discussion started by: senayasma
5 Replies

6. Shell Programming and Scripting

IP address to decimal format conversion

I have a file which consist of some class 4 IP address as i want to store them in pure decimal notations instead of the given dotted decimal formats e.g.... (2 Replies)
Discussion started by: vaibhavkorde
2 Replies

7. Shell Programming and Scripting

Decimal format

Hello Friends, I have a file with below format... 1234,Hary,102.55 4567,Maria,250.40 78942,suzan,261.60 48965,Harun,179.32 And I want to check the last column. If the decimal value of the last column is below 50, I need that column else ignore that column. Can anyone help me out? (3 Replies)
Discussion started by: mziaullah
3 Replies

8. Shell Programming and Scripting

format decimal in ksh

hi all, in ksh, how do i format a decimal number (e.g 3381.19) to integer (e.g 3381). This number is average time response for server in millisec and i wanted to display it in a more readable format i.e in seconds without any decimals. thanks in advance. (2 Replies)
Discussion started by: cesarNZ
2 Replies

9. Shell Programming and Scripting

Number format conversion in UNIX

Hi All, I want to convert Scientific number format into normal number format using unix script or using any command of unix. e.g 1.55397e+09 into 1553970000 Thanks in advance Kamal (1 Reply)
Discussion started by: kamal_418
1 Replies
Login or Register to Ask a Question