I am a php noob (very) and i got following code:
<?echo getPointsColor($row["fld_points"])?>
Currently the number 1234 in the database is shown as 1,234, but i would like to have it 1234 (without as thousand seperator).
I found:
$english_format_number = number_format($number, 0, '.', '');
This would make it 1234 instead of 1,234
BUT i don't know how to change my code (first code above) to make it work. Can someone post me how the correct codechange would like like?
Info, here is the the function:
function getPointsColor($points_to_color)
{
$myp = intval($points_to_color);
if($myp <= 0)
$myp = "<font color='#a71212\'>".number_format($myp,'\'')."</font>";
else
$myp = "<font color='#4ea712\'>".number_format($myp,'\'')."</font>";
return $myp;
}
You can do it like this:
$english_format_number = number_format($number, 2, '.', ',');
Reference
The third parameter sets the decimal separator, the fourth sets the thousands separator.
OK thanks to all!
I have edited the functions as following and it works:
function getPointsColor($points_to_color)
{
$myp = intval($points_to_color);
if($myp <= 0)
$myp = "<font color='#a71212\'>".number_format($myp, 0, '.', '')."</font>";
else
$myp = "<font color='#4ea712\'>".number_format($myp, 0, '.', '')."</font>";
return $myp;
}
Related
I'm having some trouble getting the price to show correct on my website. Currently i have a row VerkoopPP40 which is a VARCHAR input. In this row there is a price e.g. 89,5 or just 9. When I try to get these values it does some unexpected things.
**** Update ****
I've just tried this code:
<?php
function formatNumber($number, $format=[], $oldDecimalSeparator=",.·'", $multiplier=1)
{
if ($format) {
$format += ['numOfDecimals' => 0, 'decimalSeparator' => '.', 'thousandSeparator' => '']; # Default format
# Find decimal separator
# The decimal separator is the one that is the last and does not occur more than once
if ($letters = str_replace(' ', '', $number)) { # Replace spaces
if ($letters = preg_replace('/^-/', '', $letters)) { # Remove minus
if ($letters = preg_replace('/[0-9]/', '', $letters)) { # Get all non digits
$lastletter = substr($letters, -1); # Returns last char
if (substr_count($letters, $lastletter) == 1) {
if (strpos($oldDecimalSeparator, $lastletter) !== false)
$oldDecimalSep = $lastletter;
else
return $number;
}
}
}
}
$number = preg_replace('/[^0-9-]/', '', $number); # Remove all non digits except 'minus'
if ($oldDecimalSep)
$number = str_replace($oldDecimalSep, '.', $number); # Format to float
if ($multiplier != 1)
$number = $number * $multiplier;
# Convert float to new format
$number = number_format($number,
$format['numOfDecimals'],
$format['decimalSeparator'],
$format['thousandSeparator']
);
}
return $number;
}
This returns: 9,00 and 895,00 so the comma is in a different place right now. It's something I guess... Anyone got an idea to move the comma and remove a 0.
**** End Update ****
And echo-ed it like this:
<td><p>vanaf " . formatNumber($number, [
'numOfDecimals' => 2,
'decimalSeparator' => ',',
'thousandSeparator' => ' '
], ',.') . " p.p. <small>Excl btw</small></p></td>
If I just echo the VerkoopPP40 row it returns: €89,5 or €9.
So I googled around some and found this:
$var = $row["VerkoopPP40"];
$var = ltrim($var, '0');
$foo = $var;
$prijzen = number_format($foo, 2, ',', '');
This turns the . into a ,. But also returns €9,00 for the row that has 9 in it. But the strange thing is the row that has 89.5 in it now just returns €89,00. So somewhere in the code it rounds the numbers down.
Does anyone know how to get the price to show just €9,00 and €89,50 respectively.
I tried the following codes as well:
SELECT ROUND(VerkoopPP40,2) AS RoundedPrice
As database query. That didn't work.
$prijzen = CAST($prijzen as decimal(2,2));
Also didn't work. Any more ideas?
Don't know if this will help you, but found in the comments of the PHP doc : "To prevent the rounding that occurs when next digit after last significant decimal is 5 (mentioned by several people)..." read more
$num1 = "89,5";
$num2 = str_replace(',', '.', $num1);
$price = number_format($num2, 2, '.', '');
echo"[ $price ]";
you should use number_format but in the right way let me explain it to you
you tried this with 89.5
$prijzen = number_format($foo, 2, ',', '');
but this is written for 89,5 not for 89.5
//this will work for you
$var = $row["VerkoopPP40"];
echo 'raw output from database is :'.$var;
$var = $var / 10;
echo 'after this step the number is :'.$var;
$var = number_format($var, 2, '.', '');
echo 'after this step the number is :'.$var;
number_format(the input number, decimal places, 'the divider between whole numbers and decimals', '')
I am new to php. Can anyone tell how to change the query such that it will print 16.66. I need 16.66667 to be only 16.66.
<?php
$number = 16.6666667;
$n = number_format($number, 2);
echo $n; // 16.67
?>
I am getting output as 16.67. Thanks for any help.
Use the following code.
$number=floor(16.6666667 * 100) / 100;
This will return value without rounding the number.
$n = substr($number, 0, strrpos($number, '.') + 3);
EDIT
For any number with or without decimals, try this:
$n = number_format($number, 3);
$n = substr($n, 0, strrpos($n, '.') + 3);
$result = number_format($x, 2, '.', ','); //Will do the following correctly
115255 = 115,225.00
115255.4 = 115,225.40
115255.40 = 115,225.40
115255.455 = 115,255.46
But I need when the user enters more than 2 digits after the decimal, not to cut them into 2 decimals only and to use it as it is...
115255.455 = 115,255.455
115255.4557 = 115,255.4557
Can I do something like that?
if($x == number_format($x, 3)) //I will do it in while loop later, lets test 3 now
$result = number_format($x, 3, '.', ',');
else $result = number_format($x, 2, '.', ',');
The previous if condition never works, else only works
One not traditional way to do this:
$parts = explode(".", $x);
$integerPart = number_format($parts[0], 0, '', ',');
$result = $integerPart.".".$parts[1];
What's the simple way to remove more than 1 decimal number from source number .
for example source numbers are :
1st source number is : 56.48216585224
2nd source number is: 93
Output must be :
1st output : 56.4
2nd output: 93
numbers are not static
what's the simple way ?
If you don't want rounding, then:
$number = 56.48216585224;
echo substr($number, 0, strpos($number, '.')+2); // output: 56.4
Otherwise:
Use php round() or number_format()
http://php.net/manual/en/function.round.php
http://php.net/manual/en/function.number-format.php
Examples:
$number = 56.48216585224;
echo number_format($number, 1, '.', ''); // Output: 56.5
echo round($number, 1); // Output: 56.5
I will suggest this PHP code for your requirement:
$n = 56.48216585224;
$m = floor($n * 10) / 10; // $m = 56.4 now
You can try the following
print(quickFormat("56.48216585224"));
print(quickFormat("93"));
function quickFormat($number) {
$number = explode(".", $number);
if (isset($number[1])) {
return $number[0] . "." . $number[1]{0};
} else {
return $number[0] ;
}
}
Output
56.4
93
I hold decimals in a database using DECIMAL(10,5)
I would like to format these numbers according to a few rules:
A zero decimal should display as 0
Show a long decimal (no trailing zero's) with all of it's numbers
When possible, I would like to only show up to 2 decimal places (when there are trailing zeros)
Here are some examples:
The left side corresponds to how the number is stored in database.
The right number is how I would like to display the number in my application.
0.00000 => 0
0.51231 => 0.51231
0.12000 => 0.12
0.40000 => 0.40
0.67800 => 0.678
12.10000 => 12.10
This will work for you:
function format($x){
if(!(int)substr_replace($x, '', $dpos = strpos($x, '.'), 1))
return 0;
else
return str_pad((rtrim($x, '0')), $dpos + 3, '0');
}
Example
I would utilize the number_format function in php to actually do the formatting after you determine the amount of decimal places to the number has.
Source:
http://php.net/manual/en/function.number-format.php
Example Usage:
$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
Well here's one way (I haven't tested it yet so there may be minor errors):
$pattern = '/([0-9]+)\\.{0,1}([0-9]*?)0*$/';
$subject = 12.10000;
$matches = array();
$result = preg_match ($pattern, $subject, $matches);
$number = $matches[1];
if ($matches[2] != 0) {
$number .= '.'.$matches[2];
if ($matches[2] < 10) {
$number .= '0';
}
}
echo $number;
And here's another way (probably a little faster):
$x = 1.000;
$result = (int)$x;
$trimmed = rtrim($x, 0);
if ($trimmed[strlen($trimmed) - 1] != '.') {
if ($trimmed[strlen($trimmed) - 2] == '.') {
$result = $trimmed.'0';
} else {
$result = $trimmed;
}
}
echo $result;
I haven't used it myself, but theres the NumberFormatter class: http://php.net/manual/class.numberformatter.php as part of the Internationalization Functions for this stuff. Using that is a little more involved i think though.
I know this is an old question, but the following quick function I wrote for my own project might help someone looking for this.
function number_format_least_dp($number, $decimal_point = '.', $thousand_seperator = ','){
if (floatval($number) == (int)$number){
$number = number_format($number, 0, $decimal_point, $thousand_seperator);
} else {
$number = rtrim($number, '.0');
$number = number_format($number, strlen(substr(strrchr($number, '.'), 1)), $decimal_point, $thousand_seperator);
}
return $number;
}