Skip to content

Instantly share code, notes, and snippets.

@DASPRiD
Forked from cgmartin/gist:3004926
Created June 27, 2012 16:03
Show Gist options
  • Save DASPRiD/3005056 to your computer and use it in GitHub Desktop.
Save DASPRiD/3005056 to your computer and use it in GitHub Desktop.
Float and NumberFormatter oddity
$locale = $this->locale;
$fmt = new NumberFormatter($locale, NumberFormatter::DECIMAL);
if (intl_is_failure($fmt->getErrorCode())) {
throw new Exception\InvalidArgumentException("Invalid locale string given");
}
$parsedFloat = $fmt->parse($value, NumberFormatter::TYPE_DOUBLE, $position);
if (intl_is_failure($fmt->getErrorCode()) || $position < strlen($value)) {
$this->error(self::NOT_FLOAT);
return false;
}
@cgmartin
Copy link

F
$value: string(3) "1.3"
$parsedFloat: float(13)
$position: int(3)

F
$value: string(6) "1000.3"
$parsedFloat: float(10003)
$position: int(6)

F
$value: string(7) "1,000.3"
$parsedFloat: float(1.0003)
$position: int(7)

F
$value: string(8) "1 000,3"
$parsedFloat: float(1000.3)
$position: int(7)

F
$value: string(3) "1,3"
$parsedFloat: float(13)
$position: int(3)

F
$value: string(6) "1000,3"
$parsedFloat: float(10003)
$position: int(6)

F
$value: string(7) "1.000,3"
$parsedFloat: float(1.0003)
$position: int(7)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment