[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

PHP Validate a string is a valid currency decimal

Posted on 2010-09-10
2
Medium Priority
?
652 Views
Last Modified: 2012-05-10
Hi,

Given some user input I want to validate a string is an integer and convert this to a (foat,decimal) number to 2 d.p.

Input examples

7 ==> 7.00
70 ==> 70.00
70.3 ==> 70.30
70.35 ==> 70.35
40.995 ==> 41.00
40.994 ==> 40.99
70.54p ==> not valid
$70.00 ==> Not valid ($)
70GBP ==> Not valid (GBP)

Many thanks,




0
Comment
Question by:bigMlittleC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Assisted Solution

by:Mike D
Mike D earned 800 total points
ID: 33644566
Hi bigMlittleC,

I realize you're asking for PHP but since I found this under JS and my code should work similar in PHP here it is (I did this for a german customer hence the ',' [comma] and you would have to include a rounding function for 3 digits behind the decimal point scenario):

if(tot_price.indexOf(".") != -1) {
      fancy_tot_price = tot_price.substring(0, tot_price.indexOf(".")) + "," + tot_price.substring(tot_price.indexOf(".")+1, tot_price.length) + '0'      
}
else
      fancy_tot_price = tot_price + ',00'


Hope this helps!

greets
JD
0
 
LVL 12

Accepted Solution

by:
Mohamed Abowarda earned 1200 total points
ID: 33644598
First, you have to use is_numeric function to insure that the input is a number, then you can use strpos function to check if the string contains dot "." so you can know if it's decimal or integer.

is_numeric:
http://php.net/manual/en/function.is-numeric.php

strrpos:
http://php.net/manual/en/function.strrpos.php
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question