Paul-D Posted April 2 Share Posted April 2 (edited) Hi. I want to display a curreny with commas. I have looked on line and saw £totals.toLocaleString() but get. . . Fatal error: Uncaught Error: Call to undefined function toLocaleString() Or should I create a function to do this? Edited April 2 by Paul-D Quote Link to comment https://forums.phpfreaks.com/topic/332873-long-numbers-with-commas/ Share on other sites More sharing options...
gizmola Posted April 2 Share Posted April 2 toLocaleString is a javascript method available for most of the javascript types including the Number Type. Are you trying to display the number using javascript or rendering it out via PHP? Where/how are you getting the locale from? Is this a fixed locale in your code, or something that can vary based on the user of the system? With PHP code you typically want to have installed the Intl extension, which then provides you various functions that support locale presentation. Here's a quick example of the NumberFormatter class. <?php $fmt = new NumberFormatter('en_US', NumberFormatter::CURRENCY); $fmt->setAttribute(NumberFormatter::FRACTION_DIGITS, 2); $amt = 1234.56; echo $fmt->formatCurrency($amt, 'USD') . PHP_EOL; echo $fmt->formatCurrency($amt, 'EUR'); Quote Link to comment https://forums.phpfreaks.com/topic/332873-long-numbers-with-commas/#findComment-1663383 Share on other sites More sharing options...
Paul-D Posted April 2 Author Share Posted April 2 Don't understand what $amt = 1234.56; in this. I am in the UK. The line in the PHP here is 66. Thanks for your help. <?php // House contents Desmond O'Toole 2026-03-19 /* Add */ require ("../secure/SecurePDO.php"); session_start(); Session_Init(); $page = "Possesions Selection"; if(!isset($_SESSION["Pk"])) { header('Location: index.php'); exit; } $Pk = $_SESSION["Pk"]; if(KeyCheckX($Pk)== 0) { header('Location: index.php'); exit; } $pdo = connectDB(); $sqlContents = "SELECT * FROM `Contents` WHERE Cat = 0"; $stmt = $pdo->prepare($sqlContents); $stmt->execute(); ?> <html> <head> <meta http-equiv="refresh" content="600 url=index.php"> <title>Possesions</title> </head> <body> <h1><span style="color: #0000ff;">House contents</span></h1> <table border="1" width="850px" cellpadding="1"> <tr> <td width="200px"><b>Type</b></td> <td width="220px"><b>Make</b></td> <td width="150px"><b>Model</b></td> <td width="180px"><b>Serial</b></td> <td width="100px"><b>Cost</b></td> </tr> <?php $totals = 0; while($row = $stmt->fetch()) { $totals += (float)$row["Cost"]; ?> <tr> <td><?=$row["Type"]?> </b></td> <td><?=$row["Make"]?> </td> <td align="right"><?=$row["Model"]?> </td> <td align="right"><?=$row["Serial"]?> </td> <td align="right">£<?=$row["Cost"]?> </td> </tr> <?php }?> <tr> <td><span style="color: #0000ff;"><b>Total</b></span></td> <td></td><td></td><td></td> <td align="right"><span style="color: #0000ff;"> <b>£<?php echo $totals?> /* THIS LINE is currently £10230.51 </b> </span> </td> <tr> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/332873-long-numbers-with-commas/#findComment-1663384 Share on other sites More sharing options...
gizmola Posted April 3 Share Posted April 3 That was just an example amount to demonstrate how the INTL Numberformatter class works. You pass the number to be formatted, and the ISO 4217 Code and the currency string will be rendered for you. In your case, the Pound Sterling ISO 4217 Code is 'GBP'. So this line of code: <b>£<?php echo $totals?> /* THIS LINE is currently £10230.51 Would be replaced with this: <?php $fmt = new NumberFormatter('en_GB', NumberFormatter::CURRENCY); $fmt->setAttribute(NumberFormatter::FRACTION_DIGITS, 2); ?> <b><?php echo $fmt->formatCurrency($totals, 'GBP'); ?></b> You will see something like this: £10,230.51 I will say once more that this requires the INTL extension installed. Instructions for that are here: https://www.php.net/manual/en/book.intl.php Quote Link to comment https://forums.phpfreaks.com/topic/332873-long-numbers-with-commas/#findComment-1663390 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.