-2

I am making a converter from GPS Degrees, Decimal Minutes to Degrees, Minutes, Seconds since I have no found one on the internet.

Can you please tell me how to get rid of the numbers after a decimal in the calculated total field?

<HEAD>

<TITLE>Javascript Calculator</TITLE>

<SCRIPT language="javascript" type="text/javascript">
function multiply()
{
a=Number(document.calculator.number1.value);
b=.060;
c=a*b;
document.calculator.total.value=c;
}
</SCRIPT>

</HEAD>

<BODY>
<FORM name="calculator">

Decimal: <INPUT type="text" name="number1"> <br>
<INPUT type="button" value="Calculate" onclick="javascript:multiply();"><br>
Seconds: <INPUT type="text" name="total">
</FORM>
</BODY>
Jc Meyer
  • 17
  • 9
  • 4
    You can use `Math.floor()` or `Math.round()` – Pointy Feb 27 '15 at 15:06
  • possible duplicate of [Round to at most 2 decimal places in JavaScript](http://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-in-javascript) – Patsy Issa Feb 27 '15 at 15:08
  • `parseInt(c,10)` also works. Note you have a trailing parenthesis at `c)` that should be removed. – j08691 Feb 27 '15 at 15:09
  • You can also use the >> operator along with a param of 0 to force a number to an int.E.g `console.log( 3.1415926 >> 0 );`produces the output `3`. – enhzflep Feb 27 '15 at 15:14
  • Im not a javascript guy... im doing this because no one on the internet has created this exact type of GPS calculator and it is needed for our 911 system. So I don't know where to put "parseINT" or "Math.round()" – Jc Meyer Feb 27 '15 at 15:24

3 Answers3

1

The following should work out for you. Changed a few things from your original but that is only because id works better than name as name bring a nodelist.

<HEAD>

<TITLE>Javascript Calculator</TITLE>

</HEAD>

<BODY>
<FORM name="calculator">Decimal:
    <INPUT type="text" id="number1">
    <br>
    <INPUT type="button" value="Calculate" id="calculate">
    <br>Seconds:
    <INPUT type="text" id="total">
</FORM>

<script>
// put your script tags at the bottom of the page
// first get the calculate button by its Id.
var calculate = document.getElementById('calculate');
// then add an event listener to it that calls multiply function when clicked
calculate.addEventListener('click', multiply, false);

// this multiply function is called when the button is clicked
function multiply() {
    // get the inputs value then the total input fields by id
    var a = document.getElementById('number1').value;
    var total = document.getElementById('total');

    var b = 0.060;
    // Math.floor is used to round down since you just want to remove the numbers after decimal.
    var c = Math.floor(a * b);

    // set the value of total
    total.value = c;
}
</script>
</BODY>
Ally Jr
  • 1,055
  • 1
  • 14
  • 27
0
Math.round(x) //x being your number you wish to 'round

JSFiddle : http://jsfiddle.net/j37m78a4/2/

I have commented out the code that provided an answer with decimal point values. Uncomment that and comment out the line below it to see what the original answer was. :)

If you don't know where to put the code I recommened reading a Javascript book or going through online tutorials to get the basics down first. Starting off by jumping straight in to a project is good, but basics need to be known before doing this. :)

AJ_91
  • 581
  • 4
  • 16
  • I have no idea where to put this code. I am not a java guy. This is just the only way that I can think of to create a web based calculator for this type of GPS calculation which currently does not exist on the internet. I've searched everywhere. So whoever answers, can you please tell me where to put it in the code that I have given? Thank you very much. – Jc Meyer Feb 27 '15 at 15:27
  • java and javascript are totally different. what you have here is 'javascript' plus HTML. I have updated my answer with a link to working code :) hope that helps. @JcMeyer – AJ_91 Feb 27 '15 at 15:47
  • Thank you!!!! You are a lifesaver! This will be helping dispatchers receive GPS coordinates from the U.S. Coast Guard from Degrees, Decimal Minutes and convert them to Degress, Minutes, Seconds! It will help them find the exact coordinates to give to our EMS helicopter. Thank you again! – Jc Meyer Feb 27 '15 at 15:55
0

Fastest is bitwise shift with right shift operator. c = 5.674447>>0

Radio
  • 2,810
  • 1
  • 21
  • 43