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

  1. ZendTest\Validator\FloatTest::testPhpLocaleDeStringType with data set #3 ('1.3', false)
    Failed asserting that true matches expected false.

/Users/cmartin/Sites/zf2/tests/Zend/Validator/FloatTest.php:169

  1. ZendTest\Validator\FloatTest::testPhpLocaleDeStringType with data set #4 ('1000.3', false)
    Failed asserting that true matches expected false.

/Users/cmartin/Sites/zf2/tests/Zend/Validator/FloatTest.php:169

  1. ZendTest\Validator\FloatTest::testPhpLocaleDeStringType with data set #5 ('1,000.3', false)
    Failed asserting that true matches expected false.

/Users/cmartin/Sites/zf2/tests/Zend/Validator/FloatTest.php:169

  1. ZendTest\Validator\FloatTest::testPhpLocaleFrStringType with data set #2 ('1 000,3', true)
    Failed asserting that false matches expected true.

/Users/cmartin/Sites/zf2/tests/Zend/Validator/FloatTest.php:191

  1. ZendTest\Validator\FloatTest::testPhpLocaleEnStringType with data set #3 ('1,3', false)
    Failed asserting that true matches expected false.

/Users/cmartin/Sites/zf2/tests/Zend/Validator/FloatTest.php:213

  1. ZendTest\Validator\FloatTest::testPhpLocaleEnStringType with data set #4 ('1000,3', false)
    Failed asserting that true matches expected false.

/Users/cmartin/Sites/zf2/tests/Zend/Validator/FloatTest.php:213

  1. ZendTest\Validator\FloatTest::testPhpLocaleEnStringType with data set #5 ('1.000,3', false)
    Failed asserting that true matches expected false.

/Users/cmartin/Sites/zf2/tests/Zend/Validator/FloatTest.php:213

with this code:

    $locale = $this->locale;
    $format = new NumberFormatter($locale, NumberFormatter::DECIMAL);
    if (intl_is_failure($format->getErrorCode())) {
        throw new Exception\InvalidArgumentException("Invalid locale string given");
    }

    $parsedFloat = $format->parse($value);
    if (intl_is_failure($format->getErrorCode())) {
        $this->error(self::NOT_FLOAT);
        return false;
    }

    $position = 0;
    $parsedFloat = $format->parse($value, NumberFormatter::TYPE_DOUBLE, $position);
    if (intl_is_failure($format->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