Link to home
Start Free TrialLog in
Avatar of acerola
acerola

asked on

How to change comma to point in number formating?

The problem is, in English, numbers are like this:

10,000,000.00

in Portuguese:

10.000.000,00

So, how can I change the locale/regional settings so that I can use a period for grouping digits and a comma for the decimal places? Some people here have IE in English and some use IE in Portuguese, so how do I force the setting for my javascript code despite of the client computer setting?
Avatar of handful
handful

HI dude( I am brazilian too but in this forum I post in english )

Well, the point is : Javascript's interpreter is based on the foreing aspect of the numbers..so...
the only thing you can do to pass over  it..
is to make a function that works around it only at 'delivery' time ( you cannot do operations with this format because the number will be a string.
( there is no number format like the one we usehere in brazil )

Like this :

function commaToPoints(str){
 strRegExPoint=/\./g
 strRegExComma=/\,/g
 str.replace(strRegExPoint,'#')
 str.replace(strRegExComma,'.')
 str.replace(/#/,',')
}

That is only a idea.. not tested.

Avatar of acerola

ASKER

That is pretty much what I am using right now. I made two functions. One that takes a floating point number and convert to a string in the form 1.234.567,89 and another which does the opposite. Take a look:

=======================================================

function num2bra(valor) {

  var str,str1,str2;

  str = String(Math.round(valor*100)/100);
 
  str = str.replace(/\./,",");

  str1 = String(str.match(/\d+/));
 
  if (str1 == "null")
    str1 = "0";

  str2 = "";

  for(count=str1.length-3; count >= -2; count -= 3)
    str2 = str1.substring(count,count+3) + "." + str2;

  str2 = str2.substring(0,str2.length-1);

  str1 = String(str.match(/\,\d+/));

  if (str1.length == 4)
    str2 = str2 + ",00"
  else if (str1.length == 2)
    str2 = str2 + str1 + "0"
  else if (str1.length == 3)
    str2 = str2 + str1

  return str2;

}

=======================================================

function bra2num(texto) {

  var strA;
 
  strA = String(texto);
 
  strA = String(strA.replace(/\./g,""));

  strA = String(strA.replace(/\,/g,"."));
 
  return parseFloat(strA);

}
You can either retrieve the current language being used (retrieve using:)
naigator.systemLanguage

Or you can create two seperate textboxes one for the integer numbers and the other for the floating point numbers.

Lasyt thing is that most sites that "we" create are for our own country. English users are then disposed off since we also use the , instead of the ..

CJ
Avatar of acerola

ASKER

It's no use for me knowing the system language. Here in Brazil it is a mess. Some people use Brazilian windows/ie. Some use American windows/ie. Some use American window/ie set to Brazililan regional settings, and vice-versa. But all of them need to use the brasilian standart for numbers.

A even bigger mess is my web hosting. IIS is set to brazilian standarts, but mysql server is in english. So when sending/recieving sql queries I must also do the conversion.

I need to change the browser's javascript interpretation of numbers, so that it recognizes 0,50 as a fraction and not the number 50.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
ahosang
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial