0

I have a variable

$amount = 621.00;

I need the 30% of the variable with output 186.30

When I calculate:

$amount2 = round($amount*30/100 , 2);

echo $amount2

gives the output of 186.3

how can I have output 186.30

A.V.
  • 155
  • 1
  • 1
  • 11
  • Look into [sprintf](http://php.net/manual/en/function.sprintf.php). – Mr Lister Apr 29 '15 at 19:47
  • Simply said: "How to output a floating point number with a fixed number of decimals after the point". There is no rounding involved, or you must explain which one you want. – mins Apr 29 '15 at 19:51

4 Answers4

1

number_format() should do the trick.

// english notation without thousands separator
$amount2 = number_format($amount2, 2, '.', '');
// 1234.57
David Wyly
  • 1,671
  • 1
  • 11
  • 19
1

You could use number_format for that:

$amount2 = number_format($amount * 30 / 100, 2);
echo $amount2;
stefandoorn
  • 1,032
  • 8
  • 12
0

You can use number_format

For example number_format($amount2, 2)

More details: http://php.net/manual/en/function.number-format.php

Erman Belegu
  • 4,074
  • 25
  • 39
0

use number_format($amount2,2) at the end.

See documentation here: http://php.net/manual/en/function.number-format.php

Ye.
  • 171
  • 12