SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to force 2 decimal point (money)

    function below will output price value in text field based on drop-menu option. It's working but I want it's always show value in 2 decimal point. Can anyone help me with this problem?

    function findPrice() {
    if (document.getElementById("region").value == "blank") {
    var price = document.getElementById("price");
    price.value = "<?php echo 'Choose your region to get the price'; ?>";
    }
    if (document.getElementById("region").value == "Peninsular Malaysia") {
    var price = document.getElementById("price").value = "$" + price.toFixed(2);;
    price.value= '<?php echo $PM_Price; ?>';
    }
    if (document.getElementById("region").value == "Sabah/Labuan") {
    var price = document.getElementById("price");
    price.value = '<?php echo $SL_Price; ?>';
    }
    if (document.getElementById("region").value == "Sarawak") {
    var price = document.getElementById("price");
    price.value = '<?php echo $SR_Price; ?>';
    }
    }

  2. #2
    SitePoint Member
    Join Date
    Nov 2011
    Location
    Montreal, Canada
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Monetary values should always come from the server-side. Try this in your PHP:

    PHP Code:
    echo number_format(552); 

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,603
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    var price = document.getElementById("price").value = "$" + price.toFixed(2);;
    price.value= '<?php echo $PM_Price; ?>';

    is setting price to document.getElementById("price").value so the second line effectively reads

    document.getElementById("price").value.value = '<?php echo $PM_Price; ?>';

    and the value.value is undefined.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually my script was like this. This script nothing doing with decimal point. What I should do to make the price value will be shown in decimal points?

    function findPrice() {
    if (document.getElementById("region").value == "blank") {
    var price = document.getElementById("price");
    price.value = "<?php echo 'Choose your region to get the price'; ?>";
    }
    if (document.getElementById("region").value == "Peninsular Malaysia") {
    var price = document.getElementById("price");
    price.value= '<?php echo $PM_Price; ?>';

    }
    if (document.getElementById("region").value == "Sabah/Labuan") {
    var price = document.getElementById("price");
    price.value = '<?php echo $SL_Price; ?>';
    }
    if (document.getElementById("region").value == "Sarawak") {
    var price = document.getElementById("price");
    price.value = '<?php echo $SR_Price; ?>';
    }
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •