Delete digits after two decimal points, without rounding the value

Asked
Active3 hr before
Viewed126 times

7 Answers

valuedelete
90%

what i want is to delete all decimal points after 2 digits.,but when i use round php function i got round but i dont need round, i just need to delete all digits after 2 decimal simple.,A simple function to follow would be "If greater than 0 floor, else ceil", using a multiplier to raise it above the decimal point temporarily whilst doing it:,If you don't want to round the number but you want to remove the decimal places you can use "substr" method

number_format - Incorrect. (rounds)
Try using number format:

$var = number_format($var, 2, '.', ''); // Last two parameters are optional
echo $var;
// Outputs 2.50

If you want it to be a number, then simply type-cast to a float:

$var = (float) number_format($var, 2, '.', '');

sprintf - incorrect. (sprintf also rounds)
If not rounding the number is important, then per the answer below, use sprintf:

$var = sprintf("%01.2f", $var);

floor, with some math, will come close to doing what you want:

floor(2.56789 * 100) / 100; // 2.56

Where 100 represents the precision you want. If you wanted it to three digits, then:

floor(2.56789 * 1000) / 1000; // 2.567

However, this has a problem with negative numbers. Negative numbers still get rounded, rather than truncated:

floor(-2.56789 * 100) / 100; // -2.57

So a fully robust solution requires a function:

function truncate_number($number, $precision = 2) {
   // Zero causes issues, and no need to truncate
   if (0 == (int) $number) {
      return $number;
   }
   // Are we negative?
   $negative = $number / abs($number);
   // Cast the number to a positive to solve rounding
   $number = abs($number);
   // Calculate precision number for dividing / multiplying
   $precision = pow(10, $precision);
   // Run the math, re-applying the negative value to ensure returns correctly negative / positive
   return floor($number * $precision) / $precision * $negative;
}

Results from the above function:

echo truncate_number(2.56789, 1); // 2.5
echo truncate_number(2.56789); // 2.56
echo truncate_number(2.56789, 3); // 2.567

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

Use the PHP native function bcdiv

echo bcdiv(2.56789, 1, 1); // 2.5
echo bcdiv(2.56789, 1, 2); // 2.56
echo bcdiv(2.56789, 1, 3); // 2.567
echo bcdiv(-2.56789, 1, 1); // -2.5
echo bcdiv(-2.56789, 1, 2); // -2.56
echo bcdiv(-2.56789, 1, 3); // -2.567
load more v
88%

Hack solution would be to multiply by 100, take the integer value, divide by 100.,Formal solution would be to just format the number to 2 decimal points. That should truncate. ,What should i do with num1 to delete digits after two decimal point without rounding its value. I need output as 10.12 ,Built on Forem — the open source software that powers DEV and other inclusive communities.

var num1 = 10.12345678
load more v
72%

what i want is to delete all decimal points after 2 digits.,To simply "truncate" a number, bcdiv($var, 1, 2); where 2 is the number of decimals to preserve (and 1 is the denomenator - dividing the number by 1 allows you to simply truncate the original number to the desired decimal places),but when i use round php function i got round but i dont need round, i just need to delete all digits after 2 decimal simple.,number_format rounds the number

i have value in php variable like that

$var = '2.500000550';
echo $var

like now value of variable will be

$var = '2.50';
echo $var
load more v
65%

i have value in php variable like that,like now value of variable will be,what i want is to delete all decimal points after 2 digits.,but when i use round php function i got round but i dont need round, i just need to delete all digits after 2 decimal simple.

i have value in php variable like that

$var = '2.500000550';
echo $var

like now value of variable will be

$var = '2.50';
echo $var
load more v
75%

VueJS Filter Currency Rounding 2 decimal places Example ,As I will cover this Post with live Working example to develop Setting number of decimal places of a number with no rounding., so the Delete digits after two decimal points, without rounding the value for this example is following below php 2 decimal places without rounding.,Vuejs Filter Currency Round to 2 decimal places ,There are the Following The simple About php Two Decimal Places without rounding off Full Information With php round to Two decimal places Example and round function in php source code.

$my_number = 0.9999999;
echo number_format(floor($my_number * 100) / 100, 2); //Returns 0.99
load more v
40%

If you want to round the number:,Some answers here did not work (top answer is round, not truncate). here is a way:,If you do NOT want to round the number:

If you want to round the number:

var num1 = 10.12345678;
var num2 = double.parse(num1.toStringAsFixed(2)); // num2 = 10.12
load more v
22%

$value = 1.23456789;
$rounded_value = round($value, 2);
// 2 is the precision here we will get 1.23
load more v

Other "value-delete" queries related to "Delete digits after two decimal points, without rounding the value"