0

I have this code :

$number_1 = 0.01;
$number_2 = 3.22321;
$result = $number_1 * $number_2;

and the result is : 0.0322321

how to have decimal digits of $result same with $number_1, in this case, I'm expecting 0.03 (2 digits after comma).

update : $number_1 doesn't always have 2 digits after comma. it's random.

Saint Robson
  • 5,475
  • 18
  • 71
  • 118

5 Answers5

2

Just to add another option and after trying to remember some maths...

$number_1 = 0.01;
$number_2 = 3.22321;
$result = $number_1 * $number_2;
echo round($result,log10(1/$number_1));
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Full math answer, I think it is the better. – user2226755 Jun 23 '18 at 21:22
  • 1
    This doesn't work if the number has more decimals. https://3v4l.org/TKFea – Andreas Jun 24 '18 at 05:53
  • @Andreas not sure if a value for $number_1 of 0.01001 is in scope of what OP is asking - 0.00001 works, although even `$result = $number_1 * round($number_2);` works for this scenario. With the various up and down votes going on it seems like a not particularly clear question and I think your answer is a more generic solution (hence the upvote). – Nigel Ren Jun 24 '18 at 06:26
  • My point was just to say something is not correct with the math or the math is not possible on all numbers. If that is in or out of scope is for the OP to answer. I'm more questioning why your answer doesn't work than pointing it out as an error. Thank you for the upvote I will gladly do the same to you it is a nice solution, even if it seems to fail at some numbers. – Andreas Jun 24 '18 at 06:37
  • 1
    @Andreas, I am happy to be challenged as it's always possible to miss something, it may also give OP a heads up of potential issues. As you know I will also challenge others and prefer to do so than downvote if the answer is viable/fixable. – Nigel Ren Jun 24 '18 at 06:43
0

You want something like this :

<?php
$number_1 = 0.01;
$number_2 = 3.22321;
$result = $number_1 * $number_2;

$num_des = strlen(substr(strrchr($number_1, "."), 1));

echo round($result,$num_des);

?>
Supun Praneeth
  • 3,087
  • 2
  • 30
  • 33
0

You can calculate the number of decimals in $number_1 and use it in number_format function.

$decimals = strlen($number_1) - strpos($number_1, ".")-1;

echo number_format($result, $decimals); //0.03

https://3v4l.org/fGW5N

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • @Nigel that was a copy paste error. My browser reloaded and removed my `-1`. It's corrected now – Andreas Jun 23 '18 at 19:28
-1

EDIT : After your edit, the solution is : PHP: get number of decimal digits

Like that :

round(0.0322321, strlen(substr(strrchr($number_1, "."), 1)));

echo round(0.0322321, 2);  // 0.03

http://php.net/manual/fr/function.round.php

user2226755
  • 12,494
  • 5
  • 50
  • 73
-1

You can either use printf()/sprintf():

$val = 1.234;
printf('%.2f', $val);

or number_format():

$val = 1.234;
echo number_format($val, 2, '.', '');
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141