Jump to content

Showing an integer as currency with Jquery


Adamhumbug
Go to solution Solved by Barand,

Recommended Posts

I have numbers stored in a database in pence (or cents).

For example i have an item that costs 100 pounds stored as 10000 as this is the number of pence in 100 pounds.

When i am working with Jquery, how would i show the value as 100 pounds.

I have chosen this route based on some research with maths issues of storing it in pounds should their be decimals to calculate.

I was concidering a division by 100 but i am not sure what the recommended method for working with currency like this is for display only purposes.

Edited by Adamhumbug
Link to comment
Share on other sites

  • Solution

Does this help?

<!DOCTYPE html>
<html lang='en'>
<head>
<title>sample</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script type='text/javascript'>
  
    var prices = { "Item A":12500, "Item B":9500, "Item C":99, "Item D":9900 }
    
    function listPrices()
    {
        $.each( prices, function(k, v) {
            price = v/100
            let item = $("<li>", { "html": k + " :  &pound;" + price.toFixed(2)})
            $("#price-list").append(item)
        })
    }
</script>
<style type='text/css'>
    body {
        background-color: #fbf7e9;
    }
</style>
</head>
<body>
    <button onclick='listPrices()'>Prices</button>
    <ul id='price-list'>
    
    </ul>
   
</body>
</html>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.