Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 571
  • Last Modified:

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?
0
acerola
Asked:
acerola
1 Solution
 
handfulCommented:
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.

0
 
acerolaAuthor Commented:
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);

}
0
 
CJ_SCommented:
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
acerolaAuthor Commented:
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.
0
 
ahosangCommented:
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
0
 
moduloCommented:
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now