LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Negative numbers and decimals (https://www.linuxquestions.org/questions/linux-newbie-8/negative-numbers-and-decimals-4175440492/)

kross2100 12-07-2012 03:56 PM

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?

sycamorex 12-07-2012 03:59 PM

Which language? Can you show us your code?

kross2100 12-07-2012 04:04 PM

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!"


kross2100 12-07-2012 04:13 PM

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.

sycamorex 12-07-2012 04:23 PM

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


All times are GMT -5. The time now is 01:50 AM.