Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

float validation failure with trailing Zero #9

Open
pascal-larocque opened this issue Mar 3, 2016 · 1 comment
Open

float validation failure with trailing Zero #9

pascal-larocque opened this issue Mar 3, 2016 · 1 comment

Comments

@pascal-larocque
Copy link

Seems that strings with trailing Zero do not validate correctly

Inspekt::isFloat("1.60") === false
floatval() trims the trailing Zero
strval(1.6) !== "1.60"

@pascal-larocque
Copy link
Author

Should fix it
` /**
* Returns true if value is a valid float value, false otherwise.
*
* @param string $value
* @return boolean
*
* @Assert(10244578109.234451) === true
* @Assert('1.60') === true
* @Assert('1.00') === true
* @Assert('10244578109.234451') === false
* @Assert('10,244,578,109.234451') === false
*
* @tag validator
*/
public static function isFloat($value)
{
$locale = localeconv();
$value = str_replace($locale['decimal_point'], '.', $value);
$value = str_replace($locale['thousands_sep'], '', $value);
$value = rtrim($value,'0');
$value = rtrim($value,'.');

    return (strval(floatval($value)) === $value);
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant