Jump to content
DoyleChris98

Variable Output

Recommended Posts

I have some code that uses a button to open up a prompt then you enter a number XXX.XX as a frequency. Then convert it to BCD32 then display it.

Example 122.30 in decimal to 8752 in BCD32

Here is the code.

 

<html><head></head><script type="text/javascript"></script> <script>function COMSET(){var FREQ = Number(prompt ("Frequency"));//sets to 2 decimal placesFREQ = FREQ.toFixed(2);alert(FREQ);//gets the first 2 characters before the . and drops the 1var n1 = FREQ.substr(0, FREQ.indexOf("."));n1 = n1.substr(n1.length-2);alert(n1);//gets the last 2 characters after the .var n2 = FREQ.substr(FREQ.indexOf(".")+1,2);alert(n1 + " : " + n2);//Converts from Decimal to BCD formatvar bcd = (parseInt(n1.charCodeAt(0) - 0x30 )<<12) + (parseInt(n1.charCodeAt(1) - 0x30 )<<8) + (parseInt(n2.charCodeAt(0) - 0x30 )<<4) + parseInt(n2.charCodeAt(1) - 0x30 );document.getElementById(COM1_ASET).innerHTML=bcd;}</script><body>    <input name="COM1" type="button" id="COM1" value="COM1_SET" onClick="TEST()"><p id="COM1_ASET"><p> </body></html>

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...