I'm doing calculations on currency values, and the cents weren't adding up. When forcing to 2 decimal places, this code is simply dropping additional decimals, instead of rounding up. I've implemented a quick work-around:

--- modules/format_number/format_number.js	(revision 14610)
+++ modules/format_number/format_number.js	(working copy)
@@ -14,6 +14,8 @@
  *   The formatted number.
  */
 Drupal.formatNumber = function(number, decimals) {
+  var rounded = Math.round(number * Math.pow(10, decimals)) / Math.pow(10, decimals);
+  
   if (typeof(number) != "number") {
     number = 0;
   }
@@ -44,6 +46,8 @@
 
   // Build the decimal part of the number.
   if (decimals > 0) {
+    number_parts = (Math.abs(rounded) + "").split(".");
+    
     var decimal_part = (number_parts.length <= 1 ? "0" : number_parts[1]);
     if (decimal_part.length > decimals) {
       decimal_part = decimal_part.substr(0, decimals);

Comments

markus_petrux’s picture

Out of curiosity, are you using Drupal.formatNumber() in custom code or through Formatted Number CCK?

I'm asking because I was truncating because when used for a form element, it seemed to me the most logical way to behave. So maybe we need to add a 3rd argument to Drupal.formatNumber() to tell if we want the result truncated of rounded. Truncated would have to be default.

markus_petrux’s picture

Status: Active » Postponed (maintainer needs more info)
jonathan_hunt’s picture

Status: Postponed (maintainer needs more info) » Active

In this case I'm using it via the js API in custom code (I'm doing some client side order calculations, but want formatting like thousands separator and dollars, cents). Yep, an additional process argument might be needed, defaulting to truncated.

markus_petrux’s picture

Status: Active » Needs work

Ok, thanks. So, let's mark this issue as needs work, and I'll work on it later today or tomorrow if you haven't already.

markus_petrux’s picture

Title: Extra decimals are truncated not rounded » Add a 3rd argument to Drupal.formatNumber() to tell if we want the result truncated of rounded

Better title.

markus_petrux’s picture

Version: 6.x-1.4 » 6.x-1.x-dev
Category: bug » feature
markus_petrux’s picture

Status: Needs work » Needs review

Could you please try the following patch?

Index: format_number.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/format_number/Attic/format_number.js,v
retrieving revision 1.1.2.3
diff -u -p -r1.1.2.3 format_number.js
--- format_number.js	19 Nov 2008 16:53:22 -0000	1.1.2.3
+++ format_number.js	12 Sep 2009 08:25:35 -0000
@@ -10,16 +10,27 @@
  *   The number being formatted.
  * @param int decimals
  *   Number of decimal digits. Use -1 for any number of decimals.
+ * @param boolean truncate
+ *   TRUE to trucate the decimal part (default). FALSE to round the result.
  * @return string
  *   The formatted number.
  */
-Drupal.formatNumber = function(number, decimals) {
+Drupal.formatNumber = function(number, decimals, truncate) {
   if (typeof(number) != "number") {
     number = 0;
   }
   if (typeof(decimals) != "number") {
     decimals = 0;
   }
+  if (typeof(truncate) == "undefined") {
+    truncate = true;
+  }
+
+  // Round the number to the specified number of decimals if requested to.
+  // Otherwise, the decimal part will be trucated.
+  if (decimals > 0 && !truncate) {
+    number = Math.round(number * Math.pow(10, decimals)) / Math.pow(10, decimals);
+  }
 
   // Obtain the sign and separate integer/decimal parts.
   var minus_sign = (number < 0 ? "-" : "");
markus_petrux’s picture

Status: Needs review » Fixed

Committed to CVS. Thanks!

http://drupal.org/cvs?commit=265106

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

hanno’s picture

This patch has changed the optional behavior of formatnumber.js, but not for function format_number($number), right?

Also, shouldn't round be the expected default for numbers? (like in Excel, Numbers and the default cck number)