Solved

How to change comma to point in number formating?

Posted on 2002-06-15
6
566 Views
Last Modified: 2008-01-16
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
Comment
Question by:acerola
6 Comments
 
LVL 1

Expert Comment

by:handful
ID: 7081122
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
 
LVL 1

Author Comment

by:acerola
ID: 7081140
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
 
LVL 22

Expert Comment

by:CJ_S
ID: 7081872
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Author Comment

by:acerola
ID: 7082707
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
 
LVL 12

Expert Comment

by:ahosang
ID: 8285386
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
 

Accepted Solution

by:
modulo earned 0 total points
ID: 8360673
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question