The moose likes HTML, CSS and JavaScript and the fly likes Number Conversion to German Format!! Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Practical Unit Testing with TestNG and Mockito this week in the Testing forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "Number Conversion to German Format!!" Watch "Number Conversion to German Format!!" New topic
Author

Number Conversion to German Format!!

Ravi Kumar Ravuru
Ranch Hand

Joined: Apr 18, 2002
Posts: 176
Hello Folks,
I want to convert the number to german format in Javascript.Are there any functions related to it?Please help me out.
Regards,
Ravi
Chris Baron
Ranch Hand

Joined: Mar 21, 2003
Posts: 1028
Hi Ravi,
if you mean with german Number-Format ',' instead of '.' you can use replace():

Note that the variable float is a String and only useful for displaying the value. You can't calculate correctly with it anymore.
parseFloat( float ) e.g. would return 18 because the comma and everthing behind are cut.
hth
Ravi Kumar Ravuru
Ranch Hand

Joined: Apr 18, 2002
Posts: 176
Hello Christian Baron,
Thanks for ur reply.But my value doesn't have commas.I want to convert it into German Format.
Regards,
Ravi
Chris Baron
Ranch Hand

Joined: Mar 21, 2003
Posts: 1028
I want to convert it into German Format

What do you mean with that? Date-Format?
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
How do you define german format, Ravi
Afaik javascript does not have fancy i18n capabilities like Java (or am I missing something, like the other day when I realized that JavaScript has regexp???)
Christian and me are german, so you can see us as german number experts.
American number: 1,423.30
German number: 1.423,30
You could work with the replace functionaliy Christian mentioned.
Ravi Kumar Ravuru
Ranch Hand

Joined: Apr 18, 2002
Posts: 176
Hello Axel Janssen,
My value is 1423234
German number: 1.423.234
I want to convert my value to German number in JavaScript...are there any functions for the same.
Regards,
Ravi
:roll:
Chris Baron
Ranch Hand

Joined: Mar 21, 2003
Posts: 1028
are there any functions for the same

Yes, if you write them yourself . Could look like:
Ravi Kumar Ravuru
Ranch Hand

Joined: Apr 18, 2002
Posts: 176
Hello Christian Baron,
I wish your code will work perfectly right...Thanks a lot.
Regards,
Ravi
Ravi Kumar Ravuru
Ranch Hand

Joined: Apr 18, 2002
Posts: 176
Hello Christian Baron,
I had made small changes in ur code which goes here.
function format( number )
{
var n = ""+number;
var s;//add by Ravi
// make it a Stringvar s = 0;
if( n.indexOf("-") > -1 ) s=1; // negative ?
else //add by Ravi
s=0;//add by Ravi
for( i = n.length-3; i > s; i-=3 )
//Start at index 3 from the end, decrement 3 each loop
{
n = n.substring( 0, i ) + "." + n.substring( i );
// insert dots
}
return n;
}
document.write( format(1423234) );
document.write( "<br>" + format(-1423234) );
which works perfectly.Thanks a lot.
Regards,
Ravi
 
IntelliJ Java IDE
 
subject: Number Conversion to German Format!!
 
Threads others viewed
Struts 2 + Number format
Problem in setting locale
exception
The support of multi-language in a jsp page.
<fmt:formatNumber question
developer file tools