1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Inputting numbers with decimal places, for currency etc

Discussion in 'JavaScript' started by Astroman, Nov 27, 2009.

Thread Status:
Not open for further replies.
  1. #1
    Okay, this is my third thread on my first steps into javascript. It's the same bit of code that I've added to from my pervious threads, but the issues have been diffferent each time so I thought it better to keep the threads seperate too so other people can find answers easier.

    So here is my code so far:

    <SCRIPT>
    
    window.onload = function() {
    	var myForm = document.getElementById("FuelSave");
    	myForm.onsubmit = function() {
    		var n1 = parseInt(document.getElementById("num1").value, 10); // 10 base radix to avoid errors when entry like 08 in field.
            var n2 = parseInt(document.getElementById("num2").value, 10);		
            var n3 = parseInt(document.getElementById("num3").value, 10);
    		var n4 = n1 / n3 * n2 * 4.6 / 10;
    		var n5 = n1 / n3 * n2 * 4.6 / 10 / 12;
    		document.getElementById("result").innerHTML = n4.toFixed(2);
    		document.getElementById("result_month").innerHTML = n5.toFixed(2);
    		return false;
    	}
    }
    </SCRIPT>
    Code (markup):
    I want var n2 to be a currency amount, so you can type 12.67 for instance. The above code doesn't calculate anything after the . so treats the number as 12... so I'd like to know how to get it to put the whole 12.67 into the calculation?
     
    Astroman, Nov 27, 2009 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    ah
    instead of parseInt(something, 10) do parseFloat(something);
     
    dimitar christoff, Nov 27, 2009 IP
  3. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #3
    mike: erm... depends on his doctype.

    "For XHTML documents, the name attribute is not required and instead the form tag should have an id attribute and should be referenced using document.getElementById()"

    i suppose it does not hurt to have both but still: http://www.devguru.com/technologies/xhtml/QuickRef/xhtml_attribute_id.html

    why WOULDN'T you want to access a form element directly and go through the form collection instead?
    i am curious as this seems a rather old fashioned approach...

    also, does not have to be a proper form anyway--all he does is show compuations on screen in real time, there is no actual submission. he may very well just have 3 input elements and a button with a onclick that handles this, thus no elements will be children of a form as such.
     
    dimitar christoff, Nov 28, 2009 IP
  4. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Garbage.

    Just goes to show how little you know.

    You like running your mouth at people, that much we know, but you don't know anything useful about JavaScript.
     
    Mike H., Nov 28, 2009 IP
  5. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #5
    oh come on, that's not very nice. i posted quotes and references to 3rd party articles and opinions that i have read elsewhere. asking for a simple reasoning as to why you'd want to do something like this is not garbage and in doing so i strive to learn more.

    still, this does not explain why you'd come in and go on a personal attack like so? pretty poor conduct...
     
    dimitar christoff, Nov 28, 2009 IP
  6. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I didn't intend to be nice.
    Every forum has one or two like you. They believe they "own" every thread, and that others need their "permission" to post code. They go around all day, every day, shoving their finger in everyone's face, "this is no good, that's no good."

    You code is where? WHERE? Yet you come along and shove your finger in my face.

    I've seen your posts. Garbage. You hold yourself out to be an expert, when in fact, you don't you don't even rise to the level of being a novice.

    Quick, there must be at least 2 more threads where you can post your garbage, elbow everyone else out the way, because you believe you OWN this forum.

    It's people like you, who just can't keep their mouth shut for 10 seconds, who absolutely must post in every thread, every day, that ruin forums. You drive others away with your constantly running mouth.
     
    Mike H., Nov 28, 2009 IP
  7. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I despise you and your kind. I despise the self-important twits with big mouths, who appoint themselves to stand in judgment of others. I despise you.
     
    Mike H., Nov 28, 2009 IP
  8. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #8
    fair enough. i respect your opinion and would like to apologise for offending you with my constant posting. now that my credibility is in shatters and i am exposed as the lower than a novice pretend expert that i am, i will stop posting and driving people away.

    p.s. good luck selling your javascript demos.
     
    Last edited: Nov 28, 2009
    dimitar christoff, Nov 28, 2009 IP
  9. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #9
    Thanks dimitar christoff. Mike H., thanks for the advice, I am a total Javascript novice but the code you say is crap I got from a Javascript coding website. Also you sound like a bit of a nutjob, chill out man. :)
     
    Astroman, Nov 28, 2009 IP
    dimitar christoff likes this.
Thread Status:
Not open for further replies.