chrislondon
chrislondon

Reputation: 12041

How do I truncate a decimal in PHP?

I know of the PHP function floor() but that doesn't work how I want it to in negative numbers.

This is how floor works

floor( 1234.567); //  1234
floor(-1234.567); // -1235

This is what I WANT

truncate( 1234.567); //  1234
truncate(-1234.567); // -1234

Is there a PHP function that will return -1234?

I know I could do this but I'm hoping for a single built-in function

$num = -1234.567;
echo $num >= 0 ? floor($num) : ceil($num);

Upvotes: 20

Views: 34879

Answers (6)

Muhammad Dyas Yaskur
Muhammad Dyas Yaskur

Reputation: 8118

another hack is using prefix ~~ :

echo ~~1234.567; // 1234
echo ~~-1234.567; // 1234

it's simpler and faster

Tilde ~ is bitwise NOT operator in PHP and Javascript

Double tilde(~) is a quick way to cast variable as integer, where it is called 'two tildes' to indicate a form of double negation.

It removes everything after the decimal point because the bitwise operators implicitly convert their operands to signed 32-bit integers. This works whether the operands are (floating-point) numbers or strings, and the result is a number

reference:

Upvotes: 2

pablorsk
pablorsk

Reputation: 4286

Truncate floats with specific precision:

echo bcdiv(2.56789, 1, 1);  // 2.5
echo bcdiv(2.56789, 1, 3);  // 2.567
echo bcdiv(-2.56789, 1, 1); // -2.5
echo bcdiv(-2.56789, 1, 3); // -2.567

This method solve the problem with round() function.

Upvotes: 12

Manoj Yadav
Manoj Yadav

Reputation: 6612

Yes intval

intval(1234.567);
intval(-1234.567);

Upvotes: 28

Mike T
Mike T

Reputation: 1296

You can shift the decimal to the desired place, intval, and shift back:

function truncate($number, $precision = 0) {
   // warning: precision is limited by the size of the int type
   $shift = pow(10, $precision);
   return intval($number * $shift)/$shift;
}

Note the warning about size of int -- this is because $number is potentially being multiplied by a large number ($shift) which could make the resulting number too large to be stored as an integer type. Possibly converting to floating point might be better.

You could get fancy with a $base parameter, and sending that to intval(...).

Could (should) also get fancy with error/bounds checking.

An alternative approach would be to treat number as a string, find the decimal point and do a substring at the appropriate place after the decimal based on the desired precision. Relatively speaking, that won't be fast.

Upvotes: 0

Nikhil Mohan
Nikhil Mohan

Reputation: 891

Also you can use typecasting (no need to use functions),

(int) 1234.567; //  1234
(int) -1234.567; // -1234

http://php.net/manual/en/language.types.type-juggling.php

You can see the difference between intval and (int) typecasting from here.

Upvotes: 4

Ahmet Mehmet
Ahmet Mehmet

Reputation: 288

you can use intval(number); but if your number bigger than 2147483648 (and your machine/os is x64) all bigs will be truncated to 2147483648. So you can use

if($number < 0 )
$res = round($number);
else
$res = floor($number); 

echo $res;

Upvotes: 1

Related Questions