Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Am I really that stupid? [no don't answer]

I want to format dates and times to LOCALE format, so a UK machine would show 30/07/2012 and 1,234.56, an American 07/30/2012 and 1,234.56, and a Hungarian 2012.07.30. and 1 234,56.

The date is simple:
JavaScript
var dateSting = new Date(2012,07,30,0,0,0).toLocaleDateString(); // or something


Now, is there an easy, quick and RELIABLE way to format a number to the local style?


** EDIT **

Me bad! I want to format a NUMBER, the date part is okay.
Posted
Updated 30-Jul-12 0:11am
v2

toLocaleString[^]

you were close.
 
Share this answer
 
Comments
Nagy Vilmos 30-Jul-12 4:07am    
I'll have to ding you there. toLocaleString() gives the date & time, toLocaleDateString() gives just the date, which I am more interested in and toLocaleTimeString() just the time part.
But the part I am flummoxed by is a similar thing for numbers.
C#
DateTime date=DateTime.Now;
        //Sets the CurrentCulture property to U.S. English.
        System.Globalization.CultureInfo c2 = new System.Globalization.CultureInfo("en-US");
        Label1.Text = date.ToString("d",c2);


C#
// Creates a CultureInfo for German in Germany.
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("de-DE");
Label2.Text = date.ToString("d",ci);


set For
us = CultureInfo("en-US");
british = CultureInfo("en-GB");
danish = CultureInfo("da");


and for more CultureInfo http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.71).aspx[^]
 
Share this answer
 
v2
Comments
Nagy Vilmos 30-Jul-12 4:09am    
As I said to CG, I am pretty okay with the date part. In fact the toLocale* methods are more than enough as I am only interested in formatting to the machines language and/or location.

Where I am struggling is to find a decent number formatting routine.
I'm not terribly confident that my answer wont be met with rolling eyes or hostility - it certainly is fairly sloppy - though it does appear to at least work.

I guess you could always (shudder) examine the returned string representation of the date as a means of guessing/determining the appropriate routine to use to convert the number.

When run, the output is
3,141,592.653589793
3141592,653589793


HTML
<!DOCTYPE html>
<html>
<head>
<script>
    function convNum()
    {
    	var inNum = 1000000 * 3.141592653589793;
    	document.getElementById('ukUs').innerHTML = toUkUs(inNum);
    	document.getElementById('hung').innerHTML = toHung(inNum);
    }
    
    function toHung(num)
    {
    	var str = String(num);
    	return str.replace('.', ',');
    }
    
    function toUkUs(num)
    {
    	var str = thousandSeparator(num,',');
    	var n=str.indexOf(".");
    	var preDec = str.substring(0,n);
    	var postDec ='';
    	
    	for (i=n; i<str.length; i++)
    	{
    		if (str.charAt(i) != ',')
    			postDec += str.charAt(i);
    	}
    	return preDec + postDec;
    }
    
    function thousandSeparator(n,sep) {
    	var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})'), sValue=n+'';
    	
    	if (sep === undefined) {sep=',';}
    	while(sRegExp.test(sValue)) {
    		sValue = sValue.replace(sRegExp, '$1'+sep+'$2');
    	}
    	return sValue;
    }
</script>
</head>
<body>
    <span>1000000 * &pi;</span><button onclick="convNum();">Get number</button><br>
    <div id="ukUs"></div>
    <div id="hung"></div>
</body>
</html>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900