mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Improve parseFloat function
This commit is contained in:
parent
25df7eacad
commit
328d00e235
@ -12,16 +12,17 @@
|
||||
|
||||
namespace App\Http\Controllers\ClientPortal;
|
||||
|
||||
use App\Utils\Number;
|
||||
use App\Utils\HtmlEngine;
|
||||
use Illuminate\View\View;
|
||||
use App\DataMapper\InvoiceItem;
|
||||
use App\Factory\InvoiceFactory;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\ClientPortal\PrePayments\StorePrePaymentRequest;
|
||||
use App\Repositories\InvoiceRepository;
|
||||
use App\Utils\Number;
|
||||
use App\Utils\Traits\MakesDates;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use App\Utils\Traits\MakesDates;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Contracts\View\Factory;
|
||||
use Illuminate\View\View;
|
||||
use App\Repositories\InvoiceRepository;
|
||||
use App\Http\Requests\ClientPortal\PrePayments\StorePrePaymentRequest;
|
||||
|
||||
/**
|
||||
* Class PrePaymentController.
|
||||
|
@ -98,28 +98,49 @@ class Number
|
||||
if(!$value)
|
||||
return 0;
|
||||
|
||||
$multiplier = false;
|
||||
//remove everything except for numbers, decimals, commas and hyphens
|
||||
$value = preg_replace('/[^0-9.,-]+/', '', $value);
|
||||
|
||||
if(substr($value, 0,1) == '-')
|
||||
$multiplier = -1;
|
||||
$decimal = strpos($value, '.');
|
||||
$comma = strpos($value, ',');
|
||||
|
||||
if(!$comma) //no comma must be a decimal number already
|
||||
return (float) $value;
|
||||
|
||||
// convert "," to "."
|
||||
$s = str_replace(',', '.', $value);
|
||||
|
||||
// remove everything except numbers and dot "."
|
||||
$s = preg_replace("/[^0-9\.]/", '', $s);
|
||||
|
||||
if ($s < 1) {
|
||||
return (float) $s;
|
||||
if($decimal < $comma){ //decimal before a comma = euro
|
||||
$value = str_replace(['.',','], ['','.'], $value);
|
||||
// $value = str_replace(',', '.', $value);
|
||||
return (float) $value;
|
||||
}
|
||||
|
||||
// remove all separators from first part and keep the end
|
||||
$s = str_replace('.', '', substr($s, 0, -3)).substr($s, -3);
|
||||
//comma first = traditional thousan separator
|
||||
$value = str_replace(',', '', $value);
|
||||
|
||||
return (float)$value;
|
||||
|
||||
|
||||
// if(!$value)
|
||||
// return 0;
|
||||
|
||||
if($multiplier)
|
||||
$s = floatval($s)*-1;
|
||||
// $multiplier = false;
|
||||
|
||||
return (float) $s;
|
||||
// if(substr($value, 0,1) == '-')
|
||||
// $multiplier = -1;
|
||||
|
||||
// $s = str_replace(',', '.', $value);
|
||||
|
||||
// $s = preg_replace("/[^0-9\.]/", '', $s);
|
||||
|
||||
// if ($s < 1) {
|
||||
// return (float) $s;
|
||||
// }
|
||||
|
||||
// $s = str_replace('.', '', substr($s, 0, -3)).substr($s, -3);
|
||||
|
||||
// if($multiplier)
|
||||
// $s = floatval($s)*-1;
|
||||
|
||||
// return (float) $s;
|
||||
}
|
||||
|
||||
public static function parseStringFloat($value)
|
||||
|
@ -20,6 +20,36 @@ use Tests\TestCase;
|
||||
*/
|
||||
class NumberTest extends TestCase
|
||||
{
|
||||
public function testNegativeFloatParse()
|
||||
{
|
||||
|
||||
$value = '-22,00';
|
||||
|
||||
$res = Number::parseFloat($value);
|
||||
|
||||
$this->assertEquals(-22.0, $res);
|
||||
|
||||
$value = '-22.00';
|
||||
|
||||
$res = Number::parseFloat($value);
|
||||
|
||||
$this->assertEquals(-22.0, $res);
|
||||
|
||||
$value = '-2200,00';
|
||||
|
||||
$res = Number::parseFloat($value);
|
||||
|
||||
$this->assertEquals(-2200.0, $res);
|
||||
|
||||
$value = '-2.200,00';
|
||||
|
||||
$res = Number::parseFloat($value);
|
||||
|
||||
$this->assertEquals(-2200.0, $res);
|
||||
$this->assertEquals(-2200, $res);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function testConvertDecimalCommaFloats()
|
||||
{
|
||||
@ -123,4 +153,22 @@ class NumberTest extends TestCase
|
||||
|
||||
$this->assertEquals(7.99, $converted_amount);
|
||||
}
|
||||
|
||||
public function testMultiCommaNumber()
|
||||
{
|
||||
$amount = '100,100.00';
|
||||
|
||||
$converted_amount = Number::parseFloat($amount);
|
||||
|
||||
$this->assertEquals(100100, $converted_amount);
|
||||
}
|
||||
|
||||
public function testMultiDecimalNumber()
|
||||
{
|
||||
$amount = '100.1000.000,00';
|
||||
|
||||
$converted_amount = Number::parseFloat($amount);
|
||||
|
||||
$this->assertEquals(1001000000, $converted_amount);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user