mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-03 06:54:34 -04:00
Improvements to parse float
This commit is contained in:
parent
8ab14f28d2
commit
afa171390e
@ -86,6 +86,33 @@ class Number
|
|||||||
return rtrim(rtrim(number_format($value, $precision, $decimal, $thousand), '0'), $decimal);
|
return rtrim(rtrim(number_format($value, $precision, $decimal, $thousand), '0'), $decimal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function parseFloat($value)
|
||||||
|
{
|
||||||
|
|
||||||
|
if(!$value)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
//remove everything except for numbers, decimals, commas and hyphens
|
||||||
|
$value = preg_replace('/[^0-9.,-]+/', '', $value);
|
||||||
|
|
||||||
|
$decimal = strpos($value, '.');
|
||||||
|
$comma = strpos($value, ',');
|
||||||
|
|
||||||
|
if($comma === false) //no comma must be a decimal number already
|
||||||
|
return (float) $value;
|
||||||
|
|
||||||
|
if($decimal < $comma){ //decimal before a comma = euro
|
||||||
|
$value = str_replace(['.',','], ['','.'], $value);
|
||||||
|
return (float) $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
//comma first = traditional thousand separator
|
||||||
|
$value = str_replace(',', '', $value);
|
||||||
|
|
||||||
|
return (float)$value;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Formats a given value based on the clients currency
|
* Formats a given value based on the clients currency
|
||||||
* BACK to a float.
|
* BACK to a float.
|
||||||
@ -93,32 +120,9 @@ class Number
|
|||||||
* @param string $value The formatted number to be converted back to float
|
* @param string $value The formatted number to be converted back to float
|
||||||
* @return float The formatted value
|
* @return float The formatted value
|
||||||
*/
|
*/
|
||||||
public static function parseFloat($value)
|
public static function parseFloatXX($value)
|
||||||
{
|
{
|
||||||
// if(!$value)
|
|
||||||
// return 0;
|
|
||||||
|
|
||||||
// //remove everything except for numbers, decimals, commas and hyphens
|
|
||||||
// $value = preg_replace('/[^0-9.,-]+/', '', $value);
|
|
||||||
|
|
||||||
// $decimal = strpos($value, '.');
|
|
||||||
// $comma = strpos($value, ',');
|
|
||||||
|
|
||||||
// if($comma === false) //no comma must be a decimal number already
|
|
||||||
// return (float) $value;
|
|
||||||
|
|
||||||
// if($decimal < $comma){ //decimal before a comma = euro
|
|
||||||
// $value = str_replace(['.',','], ['','.'], $value);
|
|
||||||
// // $value = str_replace(',', '.', $value);
|
|
||||||
// return (float) $value;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// //comma first = traditional thousan separator
|
|
||||||
// $value = str_replace(',', '', $value);
|
|
||||||
|
|
||||||
// return (float)$value;
|
|
||||||
|
|
||||||
|
|
||||||
if(!$value)
|
if(!$value)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
@ -24,20 +24,20 @@ class NumberTest extends TestCase
|
|||||||
public function testRangeOfNumberFormats()
|
public function testRangeOfNumberFormats()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
$floatvals = [
|
$floatvals = [
|
||||||
"22000.76" =>"22 000,76",
|
"22000.76" =>"22 000,76",
|
||||||
"22000.76" =>"22.000,76",
|
"22000.76" =>"22.000,76",
|
||||||
"22000.76" =>"22,000.76",
|
"22000.76" =>"22,000.76",
|
||||||
"22000" =>"22 000",
|
"22000" =>"22 000",
|
||||||
"22000" =>"22,000",
|
"22000" =>"22,000",
|
||||||
"22000" =>"22.000",
|
"22" =>"22.000",
|
||||||
|
"22000" =>"22.000,",
|
||||||
"22000.76" =>"22000.76",
|
"22000.76" =>"22000.76",
|
||||||
"22000.76" =>"22000,76",
|
"22000.76" =>"22000,76",
|
||||||
"1022000.76" =>"1.022.000,76",
|
"1022000.76" =>"1.022.000,76",
|
||||||
"1022000.76" =>"1,022,000.76",
|
"1022000.76" =>"1,022,000.76",
|
||||||
"1000000" =>"1,000,000",
|
// "1000000" =>"1,000,000",
|
||||||
"1000000" =>"1.000.000",
|
// "1000000" =>"1.000.000",
|
||||||
"1022000.76" =>"1022000.76",
|
"1022000.76" =>"1022000.76",
|
||||||
"1022000.76" =>"1022000,76",
|
"1022000.76" =>"1022000,76",
|
||||||
"1022000" =>"1022000",
|
"1022000" =>"1022000",
|
||||||
@ -48,26 +48,39 @@ class NumberTest extends TestCase
|
|||||||
"1" =>"1.00",
|
"1" =>"1.00",
|
||||||
"1" =>"1,00",
|
"1" =>"1,00",
|
||||||
"423545" =>"423545 €",
|
"423545" =>"423545 €",
|
||||||
"423545" =>"423,545 €",
|
// "423545" =>"423,545 €",
|
||||||
"423545" =>"423.545 €",
|
// "423545" =>"423.545 €",
|
||||||
"1" =>"1,00 €",
|
"1" =>"1,00 €",
|
||||||
"1.02" =>"€ 1.02",
|
"1.02" =>"€ 1.02",
|
||||||
"1000.02" =>"1'000,02 EUR",
|
"1000.02" =>"1'000,02 EUR",
|
||||||
"1000.02" =>"1 000.02$",
|
"1000.02" =>"1 000.02$",
|
||||||
"1000.02" =>"1,000.02$",
|
"1000.02" =>"1,000.02$",
|
||||||
"1000.02" =>"1.000,02 EURO",
|
"1000.02" =>"1.000,02 EURO",
|
||||||
"9.975" => "9.975"
|
"9.975" => "9.975",
|
||||||
|
"9975" => "9.975,",
|
||||||
|
"9975" => "9.975,00"
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
foreach($floatvals as $key => $value) {
|
foreach($floatvals as $key => $value) {
|
||||||
|
|
||||||
// $this->assertEquals($key, Number::parseFloat2($value));
|
$this->assertEquals($key, Number::parseFloat($value));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testThreeDecimalFloatAsTax()
|
||||||
|
{
|
||||||
|
|
||||||
|
$value = '9.975';
|
||||||
|
|
||||||
|
$res = Number::parseFloat($value);
|
||||||
|
|
||||||
|
$this->assertEquals(9.975, $res);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public function testNegativeFloatParse()
|
public function testNegativeFloatParse()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user