0

I have a dropdown list and it has values which are to 2 decimal places.

<label for="delivery">Delivery:</label>
<select id="delivery" name="delivery">
    <option value="1.50">Fast</option>
    <option value="2.50">Medium</option>
    <option value="3.50">Slow</option>
</select>  

How can I call this in Javascript, I know I have to use the toFixed(2) somewhere in the line below, but I'm unsure where to put it.

var delivery = parseInt($('#delivery').val());

Also, would I put it in the div in where it is outputted or in the calculation, or both?

Jonah

Jonah
  • 283
  • 2
  • 5
  • 20

3 Answers3

6

Don't use parseInt() (as it kills the decimals). Use parseFloat() then toFixed():

 var delivery = parseFloat($('#delivery').val()).toFixed(2);

Note: If you are interested in rounds with toFixed(), reference this question.

Community
  • 1
  • 1
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
0

How about

var delivery = parseFloat($('#delivery').val()).toFixed(2);
Andrew Skirrow
  • 3,402
  • 18
  • 41
0

use

var delivery = parseFloat($('#delivery').val()).toFixed(2);
Amrendra
  • 2,019
  • 2
  • 18
  • 36