I'm trying to format an integer with 5 digits using php with number format plus I'm not getting the result waiting.
Example: 35914
Expected result: 35.91
Note that I pretending limits to only 2 decimal places after the point.
My attempts:
<?php
$number = 35914;
echo $english_format_number = number_format($number)."<p>";
// 35,914
// Notação Francesa
echo $nombre_format_francais = number_format($number, 2, ',', ' ')."<p>";
// 35 914,00
// Notação Inglesa com separador de milhar
echo $english_format_number = number_format($number, 2, '.', '')."<p>";
// 35914.00
?>
I do not get exactly your problem, but I would recommend to have a look on String functions (http://php.net/manual/en/ref.strings.php).
Using the str_split function, for example, we can transform the input number in the expected output.
<?php
$input = 35914;
$editedNumber = str_split($input,2);
$editedNumber = $editedNumber[0] . "." . $editedNumber[1];
echo $editedNumber; // 35.91
?>
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
?>
the code below is supposed to format the number i am getting after running multiplication in mysql to get data in a temporary table. problem is the result of the multiplication is not following the formatting. I want the number formatted to have four decimal places and the zeros retained.
<?php
$tv = $row_cscs_report['TOTAL_VALUE'];
$vr = str_replace('.', '', $tv);
echo $vr;
?>
You can do this using number_format, specifying 4 decimal places and telling it that the decimal and thousands separators should both be empty:
$tv = 3547.66;
echo number_format($tv, 4, '', '');
Output:
35476600
you can write like this:
<?php
$tv = $row_cscs_report['TOTAL_VALUE'];
$vr = sprintf("%.4f", $tv);
echo $vr;
?>
Reference:
http://php.net/manual/pl/function.sprintf.php
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'), '.');
?>
I have a basic index.php page with some variables that I want to print in several places - here are the variables:
<?php
$firstprice = 1.50;
$secondprice = 3.50;
$thirdprice = 20;
?>
My challenge is that later in the document, when I print, I get the prices without the second '0' in the price - this is what happens:
<?php print "$firstprice";?> // returns 1.5 - not 1.50!
SO - I know how to do this with JS, but how is this done in PHP 5+? Basically I want to print the second '0' if there is already a decimal, so if the variable is equal to '3', it stays as '3', but if it's equal to '3.5' it converts to display '3.50' with a second '0', etc.
Here's a JS example - what's the PHP equivalent?
JS:
.toFixed(2).replace(/[.,]00$/, ""))
Many thanks!!
This is simple and it will also let you tweak the format to taste:
$var = sprintf($var == intval($var) ? "%d" : "%.2f", $var);
It will format the variable as an integer (%d) if it has no decimals, and with exactly two decimal digits (%.2f) if it has a decimal part.
See it in action.
Update: As Archimedix points out, this will result in displaying 3.00 if the input value is in the range (2.995, 3.005). Here's an improved check that fixes this:
$var = sprintf(round($var, 2) == intval($var) ? "%d" : "%.2f", $var);
<?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 seperator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
?>
more info here
http://php.net/manual/en/function.number-format.php
You could use
if (is_float($var))
{
echo number_format($var,2,'.','');
}
else
{
echo $var;
}
What about something like this :
$value = 15.2; // The value you want to print
$has_decimal = $value != intval($value);
if ($has_decimal) {
echo number_format($value, 2);
}
else {
echo $value;
}
Notes :
You can use number_format() to format value to two decimals
And if the value is an integer, just display it.
you can use number_format():
echo number_format($firstprice, 2, ',', '.');
Alternatively way to print
$number = sprintf('%0.2f', $numbers);
// 520.89898989 -> 520.89
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;