1

I have a value 3.9609053497942. I need value 3.9 means only one value after decimal. I have used PHP number_format and round functions but it is giving me answer 4.

user3454835
  • 113
  • 1
  • 14

5 Answers5

2

You could multiply the number by 10, floor() it, and then divide it back.

echo floor($value * 10) / 10;
alex
  • 479,566
  • 201
  • 878
  • 984
1

Try with this,

echo intval((3.9609053497942*10))/10;

or

echo floor((3.9609053497942*10))/10;
Jenz
  • 8,280
  • 7
  • 44
  • 77
0

There is so many possible solutions:

echo bcadd(3.9609053497942, 0, 1);

preg_match('/\d*\.\d/', 3.9609053497942, $matches);
echo $matches[0];
sectus
  • 15,605
  • 5
  • 55
  • 97
0

why not treat it as a string, like

$x = (string)3.96;
$y = explode(".",$x);
$result = $y[0] . "." . $y[1];
sectus
  • 15,605
  • 5
  • 55
  • 97
Will
  • 271
  • 1
  • 3
  • 12
-1

Did you tried like:

number_format(3.9609053497942, 1);

Elixir Techne
  • 1,848
  • 15
  • 20