I have number ie 1,15,200 I want change to 115200 because when I divide it, it just divide 1 (before first comma)
eg :
1,15,200 / 100
output : 0.01
suppose to output : 1152
$num = (int)str_replace(',', '', $string);
// or
$num = (int)preg_replace('/[^\d]/', '', $string);
Try this way:
$num = filter_var('1,15,200', FILTER_SANITIZE_NUMBER_INT);
English notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
Related
I am extremely new to PHP, and I am having some issues with the number_format() function.
I am performing a calculation which is, correctly, returning this result: 6215.
However, I want this value to be displayed/echoed as 62.15. I have played around with the number_format() function to no avail.
Any help would be greatly appreciated!
You have 2 potential tasks here.
1.) You want to change your number (divide by 100)
<?php
$number = 6215;
$number = $number / 100;
echo $number;
?>
Renders as 62.15
2.) You may want additional formatting to make a "pretty" number
From the docs: https://www.php.net/manual/en/function.number-format.php
// Function signature syntax
number_format(
float $num,
int $decimals = 0,
?string $decimal_separator = ".",
?string $thousands_separator = ","
)
<?php
$number = 1006215.56;
$pretty_number = number_format($number, 2, '.', '');
echo $pretty_number;
?>
Renders as 1,006,215.56
This was directly pulled from the php.net documentation, which I think is really great.
https://www.php.net/manual/en/function.number-format.php
<?php
$number = 1234.56;
// english notation (default)
$english_format_number = number_format($number);
// 1,235
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56
$number = 1234.5678;
// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
?>
I have variables of bitcoin values all rounded to 8 decimal places. eg
1.00645600
I need a way in jQuery or php to get the whole number [1], The decimal values [006456], and trailing zeros [00]. I have already tried php substr but it messed up with the results since im dealing with variables.
Simple and general solution in PHP without involving regular expressions (that is an option also):
$number = '1.00645600';
$flooredNumber = floor($number); // 1
$decimalPart = (string) (floatval($number) - $flooredNumber); // 0.006456
$decimals = str_replace('0.', '', $decimalPart); // 006456
$trailingZeros = str_replace(rtrim($number, '0'), '', $number); // 00
substr
Returns the portion of string specified by the start and length parameters.
http://php.net/manual/en/function.substr.php
If the numbers in your string are always in the same position you can use substr() to get the desired values:
$str = '1.00645600';
echo substr($str, 0, 1)."\r\n";
echo substr($str, 2, 2)."\r\n";
echo substr($str, 2, 6)."\r\n";
Output:
1
00
006456
Perhaps, this way?
<?php
$i = '1.00645600';
echo rtrim(rtrim($i, '0'), '.');
?>
It will actually be a decimal but that is not the main point. I will have a set of numbers like:
8976
8765
3454
3453
10198
What I am wanting to do is add a decimal 2 places from the right. So the first would be 89.76 and so forth.
Can't you just multiply each by 0.01?
$formatted = number_format($unformatted_number / 100, 2, '.', '');
2 - decimal places
'.' - decimal separator
'' - thousands separator
docs for the function are here.
try this
$number = 8976;
$number = (float)$number/100;
results:
89.76
You may have to do some checking to see how many digits the number is, i.e 89768 would be devided by 1000 and so on.
Comments are available,
//the string you need to split
$string = "123456";
// read from right 2 character
$rightNums = substr($string, -2, 2);
// maximum 100 character to the left defined now
$otherNums = substr($string, -4, 100);
// pront them just with . between
echo $otherNums.".".$rightNums; ?>
hope it help much.
Try with this
$tmpString = substr("8976", 0, -2);
$finalString = str_replace($tmpString, "." . $tmpString, "8976");
echo $finalString;
i have a value that i calculate between 0 -100 ,
its usually a float number like 5.87876 , so i use number_format like :
$format_number = number_format($number, 2, '.', '');
the problem is , even the calculate number is integer like : 100
its show 100.00
but i want to display it like : 100
what is the elegant way to achive this ?
(i mean without else if ..)
This is the shortest way I know.
$digits = (is_numeric($number) && intval($number) == $number ? 0 : 2);
$format_number = number_format($number, $digits, '.', '');
The is_numeric and intval trick is taken from this SO question
so you are trying to have an accuracy of two decimal places after the dot, but suppress the .00 on integers? I'd use sprintf:
$numbers = Array(3.141, 5.87876, 10.9999, 100);
foreach( $numbers as $n ) {
$string = sprintf("%6.2f\n", $n);
$string = str_replace(".00", " ", $string);
echo $string;
}
The output is
3.14
5.88
11
100
I want that real numbers would be for example 12.92, but not 12.9241. Is it possible to do like that?
In PHP, try number_format:
$n = 1234.5678;
// Two decimal places, using '.' for the decimal separator
// and ',' for the thousands separator.
$formatted = number_format($n, 2, '.', ',');
// 1,234.57
For PHP you can use number_format(), for MySQL use the FORMAT() function.
MySQL: http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_format
FORMAT(number, 2)
Example:
mysql> SELECT FORMAT(12332.123456, 4);
-> '12,332.1235
PHP: http://php.net/manual/en/function.number-format.php
$number = 1234.5678;
$formatted_number = number_format($number, 2, '.', '');
// 1234.56
$number = 1234.5678;
$teX = explode('.', $number);
if(isset($teX[1])){
$de = substr($teX[1], 0, 2);
$final = $teX[0].'.'.$de;
$final = (float) $final;
}else{
$final = $number;
}
final will be 1234.56
You can multiply your number by 100, do a rounding of the result and then divide back by 100.
Or in php use the round function round function
$result=round(12.9241, 2);