Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

PHP - number_format question

New Here ,
Mar 05, 2010 Mar 05, 2010

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?

TOPICS
Server side applications
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Mar 07, 2010 Mar 07, 2010

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.

stripNum.jpg

I have also attached the original PHP code.

Translate
LEGEND ,
Mar 06, 2010 Mar 06, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 06, 2010 Mar 06, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 06, 2010 Mar 06, 2010

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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 06, 2010 Mar 06, 2010

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); }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 06, 2010 Mar 06, 2010

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)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 06, 2010 Mar 06, 2010

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 06, 2010 Mar 06, 2010

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().

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 07, 2010 Mar 07, 2010

David,

I copied this code, into a blank document and still got a T_Variable error?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 07, 2010 Mar 07, 2010
LATEST

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.

stripNum.jpg

I have also attached the original PHP code.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines