Dyan
Dyan

Reputation: 313

How to do operations with decimals in PHP?

Well, I have here two variables with values such as 4.000,00 and 1.400,50. The problem is that operations with these variables are not working. For example, if I try to do something like:

$num1 = 4.000.00;
$num2 = 1.400.50;
$result = $num1 - $num2;

echo "$result";

This not works. I have a syntax error.
I've tried using bc, but nothing works. Is there way to do this?

Upvotes: 0

Views: 63

Answers (1)

John Conde
John Conde

Reputation: 219794

Your float numbers are invalid. Only one decimal per float (and commas are not allowed):

$num1 = 4000.00;
$num2 = 1400.50;
$result = $num1 - $num2;

echo $result;

Demo

FYI, the quotes around $result is unnecessary so I removed them.

Upvotes: 2

Related Questions