Skip to content
Advertisement

convert number with decimals without

I make a query to mysql and I get this number:

26,613,080

and I need to convert that number to:

266,130 or 266.130 (it does not matter if it is , or . )

I was doing some tests with number format or removing the last 2 numbers but I can’t get it to stay as I need.

Thanks

Advertisement

Answer

If you have the intl extension installed, you can use the NumberFormatter class for this.

$input = "26,613,080";

$numberFormatter = new NumberFormatter("en_EN", NumberFormatter::DECIMAL);
$numberValue = $numberFormatter->parse($input);

var_dump($numberValue);
// float(26613080)

// I'm using TYPE_INT32 because that removes the decimal digits without rounding.
$output = $numberFormatter->format($numberValue / 100, NumberFormatter::TYPE_INT32);

var_dump($output);
// string(7) "266,130"
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement