...

View Full Version : Display values without decimals?



Gil
07-08-2002, 05:57 PM
Hi There,

If I make a calculation for example:

$a = 1;
$b = 1.001;

$answer = $a * $b;

Can I possibly echo $answer without the .decimals?

If so, please tell me how to do it!

Thanks a lot,

Gil

ObiwanJebroni
07-08-2002, 07:33 PM
I'm pretty sure you should add an (int) in front of the variable you want. Integers do not contain decimal points, so it simply truncates the decimals. For instance:


$numbertotruncate = (int)$numbertotruncate;

Íkii
07-08-2002, 07:33 PM
$answer = round($a*$b,0);

the second parameter (the 0 ) is the decimel places returned.
see also floor(number) rounds down
and ceil(number) rounds up.

note - you might need to do
$answer = $a*$b; $answer = round($answer);

Gil
07-09-2002, 07:54 AM
Thanks a lot everyone,

Both methods seem to work. With int I have to add,0. Thanks a lot.

Like this:

<?php

$a = 1;
$b = 2.4;
$answer = round($a*$b,0);


$d = 1;
$e = 2.4;
$answer2 = floor($d*$e);
$answer3 = ceil($d*$e);
$answer4 = (int)($d*$e);

echo "<p>$a x $b = $answer (basic rounding 5+ 4-)";

echo "<p>$d x $e = $answer2 (rounded down)";

echo "<p>$d x $e = $answer3 (rounded up)";

echo "<p>$d x $e = $answer4 (with int rounded down)";

?>

Gil

firepages
07-09-2002, 04:40 PM
or even ....



<?
$a=10;$b=3.46;
echo number_format($a * $b,0);
?>

Gil
07-10-2002, 08:29 AM
Wow! More?

Bye,

Flamerule
07-10-2002, 11:51 AM
More :


<?
$a = 1.001;
$b = 1;
$result = explode(".",$a*$b);
echo = result[0];
?>

Mouldy_Goat
07-10-2002, 01:00 PM
Well if we're searching for different ways:

<?php
$a = 1.001;
$b = 1;
$result = $a * $b;
echo preg_replace('/^(\d+)\.\d+/', '\1', $result);
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum