Jump to content

Variable Output


DoyleChris98

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>

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...