Is there a simpler way to validate user input for float?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is there a simpler way to validate user input for float?
# 1  
Old 01-13-2011
Is there a simpler way to validate user input for float?

I'm trying to only read price (FLOAT (i.e 1.10, 3.14, etc etc))
If the input is just an integer, I will add a .00 behind. (i.e 3 becomes 3.00 , 20 becomes 20.00)
If the input is without 2 decimal places, I'll add a 0. (i.e 3.1 becomes 3.10)
I tried using the below code, it works but I don't think this is the right way to do it since I'm gonna have never ending elif statements for different conditions.
Am I doing it wrongly?
Thanks in advance.

Code:
if [[ $price =~ [^0-9.] ]] ; then 
echo "Error"
 
else
 
if [[ $price == [.][0-9] ]] ; then
price="0""$price""0"
elif [[ $price == [.][0-9][0-9] ]] ; then
price="0""$price"
elif [[ $price == [0-9] ]] ; then
price="$price.00"
elif [[ $price == [0-9][.] ]] ; then
price="$price""00"
elif [[ $price == [0-9][0-9] ]] ; then
price="$price.00"
elif [[ $price == [0-9][0-9][.] ]] ; then
price="$price""00"
elif [[ $price == [0-9][0-9][.][0-9] ]] ; then
price="$price""0"
fi
 
fi

# 2  
Old 01-13-2011
Use printf:
Code:
price=$(printf "%0.2f" $price)

# 3  
Old 01-13-2011
Quote:
Originally Posted by Franklin52
Use printf:
Code:
price=$(printf "%0.2f" $price)


Thanks , but can you kindly explain what this code does?
Rarely used printf.
# 4  
Old 01-13-2011
few things come to mind

a) Using the bc function. That is the calculator function built-in to most unix flavors. I will have to ponder this a bit.
b) Use awk and printf to adjust.
c) You are really only concerned with three situations - two decimals, one decimal, no decimal. If you go that route, use wildcards for numbers to the left of the decimal point.
# 5  
Old 01-13-2011
Code:
# x="sed '/[0-9][0-9]*\.*[0-9]*/s/\(.*\)\.\(.\)$/\1.\20/;/^[0-9][0-9]*$/s/\(.*\)/\1.00/'"

Code:
# a=12 ; echo $a|eval $x
12.00
# a=12.1 ; echo $a|eval $x
12.10


Last edited by ygemici; 01-13-2011 at 11:32 AM..
# 6  
Old 01-13-2011
Quote:
Originally Posted by andylbh
Thanks , but can you kindly explain what this code does?
Rarely used printf.
Code:
printf "%0.2f" $price

Prints a value in 2 decimal places.
This User Gave Thanks to Franklin52 For This Post:
# 7  
Old 01-17-2011
ksh93
Code:
typeset -F2 price
price=100
echo $price

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to validate user's input..?

$Input_filename=$ARGV; if (!-d $Input_filename && ! -e $Input_filename) { print "USAGE: Please enter '$ABCD/def/dsed.txt' as an arguement \n"; exit; } 1. Input Is suppose to be something like "$ABCD/def/dsed.txt". if the input is wrong the script should throw an ERROR message.... (2 Replies)
Discussion started by: Rashid Khan
2 Replies

2. Shell Programming and Scripting

[bash] how is proper way to validate user input

hi all, i have a script that need user input provide all variables that needed to complete a job. this is my current script: echo "type file source and it full path :" read INPUTFILE if || ; then echo "ERROR: you didn't enter a file source or file source is not... (2 Replies)
Discussion started by: makan
2 Replies

3. Shell Programming and Scripting

Another validate input Question.

I'm writing a bash shell script to 'help' me post to susepaste (I can NEVER remember the time options). Here's the code: #!/bin/bash ########## # # Project : personal script. # Started : Wed Aug 03, 2011 # Author : Habitual # Description : susepaste c-li script with user... (5 Replies)
Discussion started by: Habitual
5 Replies

4. Shell Programming and Scripting

How to validate input parameters?

Hi, I wonder how I can know if the input parameters to the script are numbers or text Thanks (11 Replies)
Discussion started by: Gengis-Kahn
11 Replies

5. UNIX for Dummies Questions & Answers

BASH validate user input

Hey, im trying to validate a user input and need some help. The input needs to be just a single letter. Im using a case to so this eg: read answer case $answer in *) echo "OK" ;; *) echo "This is a number" read answer ;; *) echo... (2 Replies)
Discussion started by: 602chrislys
2 Replies

6. Shell Programming and Scripting

float input

how to input float data type in bash shell programming in linux? I am new to it so unaware to use the commands plz help me out. thank you. (6 Replies)
Discussion started by: purva
6 Replies

7. UNIX for Dummies Questions & Answers

input a float data and add them in shell in linux

I want to input a float data and add them in shell in linux. I am new user of it can anyone tell me how I can do it? thanx (5 Replies)
Discussion started by: purva
5 Replies

8. Shell Programming and Scripting

validate input

the user inputs names that have to be inside square brackets I want to check if the user puts the brackets and if not ask him to re-enter the names (9 Replies)
Discussion started by: DDoS
9 Replies

9. Shell Programming and Scripting

How to validate input values

Hi How would i validate value of a variable whether it is number,date or string Thanks in advance Sas (5 Replies)
Discussion started by: SasDutta
5 Replies

10. Shell Programming and Scripting

validate input from user for file name

Hello, This may have been addressed already somewhere, however I am looking for the easiest/shortest way to validate a response from a user for a file name. The file name should not have any of the following characters ~`!@#$%^&*()_-+={|\:;"'<,>.?/ Further the response should not have any... (2 Replies)
Discussion started by: jerardfjay
2 Replies
Login or Register to Ask a Question