# Convert Excel formula to Javascript

• 06-21-2013, 03:59 AM
pc_mac
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!
• 06-21-2013, 04:52 AM
celinsmith111
use if else condition in nested form, try it may help you
• 06-21-2013, 10:49 AM
JMRKER
Uses 'onblur' to effect change, but you can alter to some other event ...
Code:

<!DOCTYPE HTML>
<html>
<title> Untitled </title>
<meta charset="utf-8">

<body>
<table border="1"><tr>
<td><input type="text" id="E9b" value=""></td>
</tr></table>

<script type="text/javascript">
v = Number(v);
if (v >= 2) { document.getElementById('E9b').value = (v * 0.05).toFixed(2); }
else { document.getElementById('E9b').value = 2; }
}
</script>

</body>
</html>

• 06-24-2013, 08:02 AM
pc_mac
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!

Quote:

Originally Posted by JMRKER
Uses 'onblur' to effect change, but you can alter to some other event ...
Code:

<!DOCTYPE HTML>
<html>
<title> Untitled </title>
<meta charset="utf-8">

<body>
<table border="1"><tr>
<td><input type="text" id="E9b" value=""></td>
</tr></table>

<script type="text/javascript">
v = Number(v);
if (v >= 2) { document.getElementById('E9b').value = (v * 0.05).toFixed(2); }
else { document.getElementById('E9b').value = 2; }
}
</script>

</body>
</html>

• 06-24-2013, 09:40 AM
JMRKER
Response in RED below ...
Quote:

Originally Posted by pc_mac
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)
So just alter the function.

I have the formula answer going into a field called "result".
You will need to show YOUR code. I assume the 'E9b' would be your 'result' field.
Is the figure entered into E9a going into 'v' variable? Yes

Many thanks for your help again...
Hope you can help me further as I cannot get the formula to work!

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:
• 06-24-2013, 11:14 AM
pc_mac
Quote:

Originally Posted by JMRKER
Response in RED 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:

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:

Quote:

if (parseInt(form. E9.value) * 0.05 > 2)
{ form.result.value = parseInt(form. E9.value) * 0.06 }
else { form.result..value = 2 }

Thanks again
• 06-24-2013, 05:48 PM
JMRKER

"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.

And look at your error console.

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.12721 seconds
• Memory Usage 2,368KB
• Queries Executed 11 (?)
Template Usage (21):
• (2)bbcode_code_printable
• (4)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates