How check value is decimal or not in php?
I need to check in PHP if user entered a decimal number (US way, with decimal point: X.XXX) Show Any reliable way to do this?
Uwe Keim 38.6k56 gold badges173 silver badges280 bronze badges asked Jul 21, 2011 at 7:22
CodeVirtuosoCodeVirtuoso 6,08012 gold badges44 silver badges62 bronze badges 6 You can get most of what you want from is_float, but if you really need to know whether it has a decimal in it, your function above isn't terribly far (albeit the wrong language):
answered Jul 21, 2011 at 7:28
cwallenpoolecwallenpoole 77.3k26 gold badges125 silver badges163 bronze badges 7 if you want "10.00" to return true check Night Owl's answer If you want to know if the decimals has a value you can use this answer. Works with all kind of types (int, float, string)
Examples:
Explanation:
Modulus operator - why not use the modulus operator? E.g. From the PHP docs:
Which will effectively destroys the op purpose, in other languages like javascript you can use the modulus operator
answered Nov 6, 2014 at 13:23
1 If all you need to know is whether a decimal point exists in a variable then this will get the job done...
This isn't a very elegant solution but it works with strings and floats. Make sure to use !== and not != in the strpos test or you will get incorrect results. answered Jan 24, 2012 at 4:07
Night OwlNight Owl 4,1284 gold badges27 silver badges37 bronze badges 1 another way to solve this: answered Jul 21, 2011 at 7:33
k102k102 7,5137 gold badges48 silver badges69 bronze badges 0 The function you posted is just not PHP. Have a look at Edit: I missed the "user entered value" part. In this case you can actually use a regular expression:
answered Jul 21, 2011 at 7:26
Felix KlingFelix Kling 767k171 gold badges1068 silver badges1114 bronze badges 2
I was passed a string, and wanted to know if it was a decimal or not. I ended up with this:
I ran a bunch of test including decimals and non-decimals on both sides of zero, and it seemed to work.
gen_Eric 217k40 gold badges295 silver badges334 bronze badges answered Oct 13, 2011 at 16:58
Jeff VJeff V 511 silver badge1 bronze badge 1
You may wish to convert the integer to a decimal with PHP, or let your database do it for you. answered Nov 20, 2013 at 14:15
rybo111rybo111 12k4 gold badges58 silver badges67 bronze badges 1 This is a more tolerate way to handle this with user input. This regex will match both "100" or "100.1" but doesn't allow for negative numbers.
answered Sep 10, 2013 at 18:05
ChaoixChaoix 1,24810 silver badges13 bronze badges
answered Jul 31, 2014 at 12:46
Van BienVan Bien 111 silver badge2 bronze badges i use this:
answered Jul 5, 2014 at 4:19
0
answered Mar 3, 2014 at 11:20
NeocortexNeocortex 6439 silver badges32 bronze badges A total cludge.. but hey it works !
answered Mar 6, 2015 at 22:22
Duane LortieDuane Lortie 1,2921 gold badge12 silver badges16 bronze badges the easy way to find either posted value is integer and float so this will help you
if you give 10 or 10.5 or 10.0 the result will be success if you define any character or specail character without dot it will give unsuccess answered Dec 25, 2019 at 11:57
abubakkar tahirabubakkar tahir 6871 gold badge11 silver badges12 bronze badges How about answered Nov 29, 2021 at 15:49
I can't comment, but I have this interesting behaviour. (tested on v. 7.3.19 on a website for php testing online) If you multiply 50 by 1.1 fmod gives different results than expected. If you do by 1.2 or 1.3 it's fine, if you do another number (like 60 or 40) is also fine.
answered Feb 6 at 18:37
1 Simplest solution is
answered Aug 24, 2017 at 10:06
Ayyaz ZafarAyyaz Zafar 1,8185 gold badges25 silver badges39 bronze badges If you are working with form validation. Then in this case form send string. I used following code to check either form input is a decimal number or not. I hope this will work for you too.
answered Dec 15, 2017 at 2:17
Adnan AhmadAdnan Ahmad 7901 gold badge10 silver badges12 bronze badges
Known Issues with the above function: 1) Does not support "scientific notation" (1.23E-123), fiscal (leading $ or other) or "Trailing f" (C++ style floats) or "trailing currency" (USD, GBP etc) 2) False positive on string filenames that match a decimal: Please note that for example "10.0" as a filename cannot be distinguished from the decimal, so if you are attempting to detect a type from a string alone, and a filename matches a decimal name and has no path included, it will be impossible to discern. answered Feb 10, 2020 at 19:24
Maybe try looking into this as well
answered Dec 23, 2012 at 4:13
Cam TullosCam Tullos 2,4771 gold badge20 silver badges17 bronze badges 0 Not the answer you're looking for? Browse other questions tagged php function decimal or ask your own question.How can I check if a number is decimal in PHP?The PHP is_numeric() function can be used to find whether a variable is numeric. The function returns true if the variable is a number or a numeric string, false otherwise.
How check value is number or not in PHP?The is_numeric() function checks whether a variable is a number or a numeric string. This function returns true (1) if the variable is a number or a numeric string, otherwise it returns false/nothing.
How do you check if a string has decimals?result = sqrt(stringContainingANumber); decimal = new RegExp("."); document. write(decimal. test(result));
|