1

I've got some Excel formulas. Something like this:

13,7%/(33.3%/6.0%)*100%

How can I do this in Javascript?

Thanx in advance!

Freek

4
  • @pimvdb No it doesn't. 100+10% usually isn't evaluated to 100.1 but 110. Personally I don't like that kind of calculation at all and always switch to real numbers. Commented Mar 26, 2011 at 14:14
  • @CodeInChaos: My bad, but your example returns 10010.00% in Excel. Commented Mar 26, 2011 at 14:16
  • 1
    What I said was based on how my old calculator handled it. Excel seems to be even more strange. Just checked and my new calculator calculated it as 100.1. So it seems that adding percentages to real numbers is very dependent on the program. Commented Mar 26, 2011 at 14:18
  • well it makes sense: 100 + 10% == 100 + 10/100 = 100.1 :) Commented Mar 26, 2011 at 14:23

3 Answers 3

4

just treat the numbers as decimals instead of percentages

var x = .137/(.333/.06)*100

Then format the result with a % to display it.

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

2 Comments

Shouldn't that last number be 1, and not 100?
@pimvdb That comment made my day
1

Move the decimal point two places to the left.

Comments

0

You could have a regexp replacing all occurences with /100.

var str = "13.7% + 50%/3%",
    regexp = /([0-9]*\.?[0-9]*)\%/;

while(regexp.test(str)) str = str.replace(regexp, "($1/100)")
// "(13.7/100) + (50/100)/(3/100)"

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.