?
Solved

Num Format in the Java

Posted on 2003-03-05
3
Medium Priority
?
596 Views
Last Modified: 2007-12-19
Hi,

I have a code in javascript:

function num_format(x, presymbol, postsymbol)
{     // format numbers with two digits
     sgn = (x < 0);
     x = Math.abs(x);
     x = Math.floor((x * 100) + .5);
     i = 3;
     y = "";
     k = 0;
     while(((i--) > 0) || (x > 0)) {
          y = (x % 10) + y;
          x = Math.floor(x / 10);
          if(i == 1) {
               y = "." + y;
          }
          if (i<1) {
               k ++;
               if (k%3 == 0) {
                    if (x>0) {
                         y = "," + y;
                    }
               }
          }
     }
     y = presymbol + y + postsymbol;
     if(sgn) {
          y = "(" + y + ")";
     }
     return(y);
}

in the java:

public String num_format(double x, String presymbol, String postsymbol) throws Exception
{

     String y = "";
     int i = 0;
     int k = 0;    
     boolean sgn = false;
//     System.out.println(x);
     
     sgn = (x < 0);
     x = Math.abs(x);
     x = Math.floor((x * 100) + .5);

     i = 3;
     y = "";
     k = 0;
     while(((i--) > 0) || (x > 0)) {
          y = (x % 10) + y;
          x = Math.floor(x / 10);
          if(i == 1) {
               y = "." + y;
          }
          if (i<1) {
               k ++;
               if (k%3 == 0) {
                    if (x>0) {
                         y = "," + y;
                    }
               }
          }
     }
//     System.out.println(y);    
     y = presymbol + y + postsymbol;
     if(sgn) {
          y = "(" + y + ")";
     }
     return(y);
}

when i try:
System.out.println(num_format(1000, "$", ""));

in the javascript is no problem can get this $1,000, but in the java, i got this answer:
$1.0,0.00.00.0.0.00.0

Can someone help me fix,thanks.
0
Comment
Question by:weesiong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8077856
pls use DecimalFormat as follows:

java.text.DecimalFormat df = new java.text.DecimalFormat("###,##0.00");
String formated_number = df.format(1000);
0
 
LVL 1

Accepted Solution

by:
gwang77 earned 400 total points
ID: 8077894
the problem is at
y = (x % 10) + y;
x%10, at javascript, it will return 0
at java, it will return 0.0, a double type.
change it to the follow is ok.
y = (int)(x % 10) + y;
0
 
LVL 7

Author Comment

by:weesiong
ID: 8077949
Thanks for your fast response.

Best Regards,
Wee Siong
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!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

765 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