How do I convert output of number_format back to numbers in PHP?
You could remove any character that is not a digit or a decimal point and parse that with floatval
:
$number = 1200.00;
$parsed = floatval(preg_replace('/[^\d.]/', '', number_format($number)));
var_dump($number === $parsed); // bool(true)
And if the number has not .
as decimal point:
function parse_number($number, $dec_point=null) {
if (empty($dec_point)) {
$locale = localeconv();
$dec_point = $locale['decimal_point'];
}
return floatval(str_replace($dec_point, '.', preg_replace('/[^\d'.preg_quote($dec_point).']/', '', $number)));
}
number_format turns float into a string php
I will sum up all the comments for you. It is the intended way of using number_format
Example:
$someFloat = 10.99; // Float 10.99
$formattedString = number_format($someFloat, 2, ','); // String: 10,99
$floatFromString = (float) $formattedString // Float: 10.99 - note that this might throw an error/warning depending on language culture used by your server
The function declaration:number_format ( float $number [, int $decimals = 0 ] ) : string
number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," ) : string
Input is a float, output is always a string.
Doc. reference
Converting number-formatted variable back to original format - PHP
$number = 500000;
$formattedNumber = number_format($number, 2);
$getBackOriginl = explode('.',$formattedNumber);
echo str_replace(',','',$getBackOriginl[0]);
number_format on 1 million return Nan0
Your number_format
PHP code works fine, however you don't want this formatted. You want to provide the plain, raw number as the data-stop
attribute.
data-stop="1164116"
PHP number_format returns 1.00
Your function returns 1,423.28
? This is not a float, as a float does never contain a comma as a thousands-separator.
PHP interprets this as 1, because it "breaks" at the comma.
Remove the comma and you are fine!
$balance = str_replace(',', '', $balance);
How to convert number to float in PHP
You can use number_format function to format the numbers for desired output.
$foo = "105.01";
$zoo = "105";
echo number_format((float)$foo, 2, '.', ''); //Output 105.01
echo number_format((float)$zoo, 2, '.', ''); //Output 105.00
php converting formatted int to int
intval()
won't work with the string you have because of the comma. You can remove the comma by using str_replace()
and then calling intval()
like so:
echo intval(str_replace(',', '', $myVal))
Show a number to two decimal places
You can use number_format():
return number_format((float)$number, 2, '.', '');
Example:
$foo = "105";
echo number_format((float)$foo, 2, '.', ''); // Outputs -> 105.00
This function returns a string.
PHP number format with integer format
Just do a loose comparison of $price
cast as integer against $price
, if they match (ie. it's a whole number), you can format to 0 decimal places:
number_format($price, ((int) $price == $price ? 0 : 2), '.', ',');
Related Topics
Jquery Ajax Form Using Mail() PHP Script Sends Email, But Post Data from HTML Form Is Undefined
PHP Send Mail to Multiple Email Addresses
Parse Xml Namespaces with PHP Simplexml
Back to Previous Page with Header( "Location: " ); in PHP
How to Replace Part of a String in PHP
Is PHP's Count() Function O(1) or O(N) for Arrays
Phpunit Mock Objects and Static Methods
Error While Using Pdo Prepared Statements and Limit in Query
How to Delete an Object's Property in PHP
Post and Get at the Same Time in PHP
How to Select Randomly with Doctrine
Will Copy-On-Write Prevent Data Duplication on Arrays
Laravel Orderby Relationship Count
PHP Get Highest Value from Array
What Does a Successful MySQL Delete Return? How to Check If Delete Was Successful