Copy link to clipboard
Copied
I want to format a number without rounding it. For example, I have a number that is 5.77890 and I just want to drop the 7890 and display 5.7, but without rounding it up. Right now I am using number_format but it's rounding it to 5.8. How can this be done?
1 Correct answer
I don't see how you can get an error by running that code. Here is a screenshot of it running in Live View. You can see the output "3.3" in Design view.
I have also attached the original PHP code.
Copy link to clipboard
Copied
If you don't want the number to be rounded up, you will need to use string manipulation functions to achieve what you want. The following custom function will do it for you:
function stripNum($num, $decplaces = 1) {
$pos = strpos($num, '.');
return substr($num, 0, $pos+1+$decplaces);
}
This finds the decimal point, and returns the value truncated to the number of decimal places specified by the second (optional) argument.
$original = 5.77890;
echo stripNum($original); // displays 5.7
echo stripNum($original, 2) // displays 5.77
If there's no decimal point, you just get the original number.
Copy link to clipboard
Copied
David, I tried this below and am getting a T_Variable error on the line $pos
= strops($num, '.'); HERE is my code:
<?php function stripNum($num, $decplaces = 1) {
$pos = strpos($num, '.');
return substr($num, 0, $pos1$decplaces); }
if ($row_getCountin12['countin12'] >= 20) {
if (((($row_getHandi['sum_hand']) - ($row_getTourd['handi'])) < 3))
{
echo stripNum($row_getHandi['sum_hand']);
}
} ?>
Thanks.
Copy link to clipboard
Copied
rickpick77665 wrote:
return substr($num, 0, $pos1$decplaces); }
You have got this:
$pos1$decplaces
It should be:
$pos+1+$decplaces
Or if you prefer to spread things out:
$pos + 1 + $decplaces
Copy link to clipboard
Copied
No I have it like that. It's giving me a T_Variable on the the $pos =
strops($num, '.'); line.
function stripNum($num, $decplaces = 1) {
$pos = strpos($num, '.');
return substr($num, 0, $pos1$decplaces); }
Copy link to clipboard
Copied
The error is not coming from the function I gave you. I have copied your code, and tested it with some dummy numbers like this:
<?php
function stripNum($num, $decplaces = 1) {
$pos = strpos($num, '.');
return substr($num, 0, $pos + 1 + $decplaces);}
$row_getCountin12['countin12'] = 25;
$row_getHandi['sum_hand'] = 3.345667;
$row_getTourd['handi'] = 1.23458;
if ($row_getCountin12['countin12'] >= 20) {
if (((($row_getHandi['sum_hand']) - ($row_getTourd['handi'])) < 3))
{
echo stripNum($row_getHandi['sum_hand']);
}
}
It outputs 3.3, as expected. The error is coming from somewhere else in your script.
By the way, I have no idea why you have so many parentheses in your nested conditional statement. This does exactly the same:
if ($row_getHandi['sum_hand'] - $row_getTourd['handi'] < 3)
Copy link to clipboard
Copied
David,
I just copied the code below and pasted it on a page and this is the error
it's giving me. I tried it on a blank page also...same thing.
Parse error: syntax error, unexpected T_VARIABLE in
/home/pickup7/public_html/pickaplayer.php on line 407
Line 407 in the code is $pos = strops($num, '.');
Why is it giving me this?
Copy link to clipboard
Copied
rickpick77665 wrote:
Line 407 in the code is $pos = strops($num, '.');
Read the code that I gave you. Also look at the PHP manual.
There is no such function in PHP as strops(). It's strpos().
Copy link to clipboard
Copied
David,
I copied this code, into a blank document and still got a T_Variable error?
Copy link to clipboard
Copied