Opened 13 years ago

Closed 13 years ago

#1678 closed defect (fixed)

dojo.i18n.number.format formatting problem

Reported by: cazzius Owned by: Adam Peller
Priority: high Milestone: 0.9
Component: General Version: 0.4
Keywords: Cc:
Blocked By: Blocking:

Description

There is a problem when formatting number with decimals and flags.places > 0.

Esemple:

var a = 0.01;

dojo.i18n.number.format(a, {places: 2}) returns "0.1"

simple fix follows

Index: number.js
===================================================================
--- number.js	(revision 6230)
+++ number.js	(working copy)
@@ -59,7 +59,10 @@
 	if (flags.places > 0){
 	//Q: Is it safe to convert to a string and split on ".", or might that be locale dependent?  Use Math for now.
 		var fract = value - Math.floor(value);
-		fract = (flags.round ? Math.round : Math.floor)(fract * Math.pow(10, flags.places));
+                fract = String((flags.round ? Math.round : Math.floor)(fract * Math.pow(10, flags.places)));
+                while (fract.length < flags.places) {
+                        fract = "0" + fract;          
+                }
 		output = output + flags.decimal + fract;
 	}

Change History (2)

comment:1 Changed 13 years ago by Adam Peller

Milestone: 0.5
Owner: changed from anonymous to Adam Peller

this is experimental (abandoned) code about to be rewritten. I'll make sure it works this time :)

comment:2 Changed 13 years ago by Adam Peller

Resolution: fixed
Status: newclosed

this should also work in the new version, with a test case to match

Note: See TracTickets for help on using tickets.