tags:

views:

72

answers:

5

I'm looking for the shortest/easiest way to achieve this.

var savings = <?php echo $list_price ?> - discount_price;

savings: 1.00

$list_price: '$10.00'; discount_price: '$9.00';

Cheers.

A: 

Not sure what you're asking here - could you post an example of what you want to achieve?

Nev Stokes
I have a currency in a PHP variable and a JavaScript variable which I need to subtract from one another. See above.
reckoner
In that case, I'd just strip anything that's non numerical or period with a simple preg_replace()
Nev Stokes
A: 

isnt currency by default represented as a "decimal" (of sorts)

$1.00 = 1.00
0.50c = 0.5
Ross
A: 

Maybe this is the answer?

var savings = parseFloat(<?php echo $list_price ?>) - discount_price;
Rocket
This did not work.
reckoner
A: 

Your question is your answer ..! Am I missing anything ?

Stewie
You cannot simply subtract a string from another string :)
reckoner
+1  A: 
var savings = parseFloat('<?php echo $list_price ?>'.substr(1)) -
  parseFloat(discount_price.substr(1));

This parses the string as a number, skipping the first characters (dollar).

Lekensteyn
Thank you! This is the only answer that worked.
reckoner
Shouldn't `parseInt` be `parseFloat`? What if `$list_price` was $12.56?
Rocket
Rocket, of course! I'm used to `parseInt`, and typed it in wrongly :|
Lekensteyn