/    Sign up×
Community /Pin to ProfileBookmark

Convert Excel formula to Javascript

Hi

Can anyone help me convert this formula to Javascript:

=IF((E9*0.05)>2, E9*0.05, 2)
(IF(logical_test,value_if_true,value_if_false))

Not sure how to go about the if statement and the value if true function in Javascript.

Hope some one can help!

Many thanks!

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@celinsmith111Jun 21.2013 — use if else condition in nested form, try it may help you
Copy linkTweet thisAlerts:
@JMRKERJun 21.2013 — Uses 'onblur' to effect change, but you can alter to some other event ...
<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Untitled &lt;/title&gt;
&lt;meta charset="utf-8"&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;table border="1"&gt;&lt;tr&gt;
&lt;td&gt;&lt;input type="text" id="E9a" value="1" onblur="adjustFormula(this.value)"&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="text" id="E9b" value=""&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;script type="text/javascript"&gt;
function adjustFormula(v) {
v = Number(v);
if (v &gt;= 2) { document.getElementById('E9b').value = (v * 0.05).toFixed(2); }
else { document.getElementById('E9b').value = 2; }
}
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@pc_macauthorJun 24.2013 — JMRKER thanks so much for your help!!

There was a mistake in my formula! Sorry about that it was supposed to be:

=IF((E9*0.05)>2, E9*0.06, 2)

I have the formula answer going into a field called "result".

Is the figure entered into E9a going into 'v' variable?

Many thanks for your help again...

Hope you can help me further as I cannot get the formula to work!

Uses 'onblur' to effect change, but you can alter to some other event ...
<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Untitled &lt;/title&gt;
&lt;meta charset="utf-8"&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;table border="1"&gt;&lt;tr&gt;
&lt;td&gt;&lt;input type="text" id="E9a" value="1" onblur="adjustFormula(this.value)"&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="text" id="E9b" value=""&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;script type="text/javascript"&gt;
function adjustFormula(v) {
v = Number(v);
if (v &gt;= 2) { document.getElementById('E9b').value = (v * 0.05).toFixed(2); }
else { document.getElementById('E9b').value = 2; }
}
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
[/QUOTE]
Copy linkTweet thisAlerts:
@JMRKERJun 24.2013 — Response in [COLOR="#FF0000"]RED[/COLOR] below ...
JMRKER thanks so much for your help!!

There was a mistake in my formula! Sorry about that it was supposed to be:

=IF((E9*0.05)>2, E9*0.06, 2)

[COLOR="#FF0000"]So just alter the function. [/COLOR]

I have the formula answer going into a field called "result".

[COLOR="#FF0000"]You will need to show YOUR code. I assume the 'E9b' would be your 'result' field. [/COLOR]

Is the figure entered into E9a going into 'v' variable? [COLOR="#FF0000"]Yes[/COLOR]

Many thanks for your help again...

Hope you can help me further as I cannot get the formula to work![/QUOTE]

What is wrong with your code. What errors are you getting in the error console?

The program works fine as published in post #3. What are you using to trigger the event?

Without seeing what you are doing, I'm no better at giving a haircut through the mail than I am at debugging your code! :eek:
Copy linkTweet thisAlerts:
@pc_macauthorJun 24.2013 — Response in [COLOR="#FF0000"]RED[/COLOR] below ...

What is wrong with your code. What errors are you getting in the error console?

The program works fine as published in post #3. What are you using to trigger the event?

Without seeing what you are doing, I'm no better at giving a haircut through the mail than I am at debugging your code! :eek:[/QUOTE]


I'm not getting any errors - nothing happens when I try to add the "E9*0.06, 2" in to the javascript code.

Where would I add this code?

I'm not fully understanding how this Javascript code works the same as the excel formula. Sorry, I hope you don't mind helping me out.

This was my 1st attempt:

if (parseInt(form. E9.value) * 0.05 > 2)

{ form.result.value = parseInt(form. E9.value) *
0.06 }

else { form.result..value = 2 }
[/QUOTE]




Thanks again
Copy linkTweet thisAlerts:
@JMRKERJun 24.2013 — Show your code!

"E9" is not an element in the code I provided.

It does not use a <form> tag.

In some places you use form.result.value, in other you use form. E9.value (note the space)

and in still another you use form.result..value ( note the double dots). Cannot be all three syntax correct.

Note also that you do not define the form in your snippet.

Show your code!

And look at your error console.
×

Success!

Help @pc_mac spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...