LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-07-2012, 04:56 PM   #1
kross2100
LQ Newbie
 
Registered: Dec 2012
Posts: 9

Rep: Reputation: Disabled
Negative numbers and decimals


I am trying to be able to get the user to give me real and whole numbers and use these numbers to calculate into other variables. Sometimes I'll have to return a negative number. How do I do this?
 
Old 12-07-2012, 04:59 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,578
Blog Entries: 1

Rep: Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033
Which language? Can you show us your code?
 
Old 12-07-2012, 05:04 PM   #3
kross2100
LQ Newbie
 
Registered: Dec 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Oops sorry

Code:
#!/bin/bash
set -xv

echo "Enter prices."
read price
least=$price
great=$price
count=0
total=0
while [ -n "$price" ]
do
  read price
  if test $price -lt $least
  then
    let least=$price
  fi
  if test $price -gt $great
  then
    let great=$price
  fi
  let total=$total+$price
  let count=$count+1
done
while [ test $total -gt 0 ]
do
  echo "Balance due is $"$total
  echo "Please enter payment."
  read payment
  let $total=$total-$payment
done
echo "Your change due is $"$total
echo "Total number of items: "$count
echo "The cheapest price: $"$least
echo "The most expensive price: $"$great
echo "Thank you for shopping at Unix Electronics!"
 
Old 12-07-2012, 05:13 PM   #4
kross2100
LQ Newbie
 
Registered: Dec 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
So basically I want the user to be able to enter a price like 8.55 without it giving me an error for it not being an integer when the calculations start to happen. So I'm guessing there is some kind of option I need, but I don't know what it is. Also, when a "payment" equals more than the "total" I don't want it to error out.
 
Old 12-07-2012, 05:23 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,578
Blog Entries: 1

Rep: Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033
Bash itself is not very good at dealing with floating point numbers. Have a look at:
http://stackoverflow.com/questions/9...n-shell-script

For your other errors:
http://stackoverflow.com/questions/4...rator-expected
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bc and exponents containing decimals and fractions. Dogs Programming 6 03-12-2013 05:27 PM
[SOLVED] How to compare negative and decimal numbers satishchahar Programming 16 09-21-2011 11:17 AM
Bourne and Decimals? kdyzsa Programming 4 05-07-2008 02:16 AM
use decimals in java? rabbit2345 Programming 1 03-08-2008 01:08 AM
how to compare decimals linuxboy69 Linux - Software 2 08-16-2004 03:43 PM


All times are GMT -5. The time now is 12:25 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration