Adamhumbug Posted June 19, 2022 Share Posted June 19, 2022 (edited) 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 June 19, 2022 by Adamhumbug Quote Link to comment https://forums.phpfreaks.com/topic/314939-showing-an-integer-as-currency-with-jquery/ Share on other sites More sharing options...
Solution Barand Posted June 19, 2022 Solution Share Posted June 19, 2022 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 + " : £" + 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> Quote Link to comment https://forums.phpfreaks.com/topic/314939-showing-an-integer-as-currency-with-jquery/#findComment-1597444 Share on other sites More sharing options...
Adamhumbug Posted June 19, 2022 Author Share Posted June 19, 2022 Yes that helps very much thank you. Quote Link to comment https://forums.phpfreaks.com/topic/314939-showing-an-integer-as-currency-with-jquery/#findComment-1597445 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.