0

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.

2
  • Give some examples of inputs and desired outputs. Commented Sep 8, 2010 at 16:55
  • @Cal Jacobson Thanks for reminding me! Commented Sep 8, 2010 at 17:11

3 Answers 3

1
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).

Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't parseInt be parseFloat? What if $list_price was $12.56?
Rocket, of course! I'm used to parseInt, and typed it in wrongly :|
0

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

$1.00 = 1.00
0.50c = 0.5

Comments

0

Maybe this is the answer?

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.