• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

java.lang package

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is there any built-in function to display the double/float number in two decimal digit format as 3.14752 to 3.14 ?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
there is no built in fuction IMO. the best thing u can do is multiply the number by 100. then cast it to an int. and then again divide it by 100 and store the result in a double
for example to find the value of
class demo
{
public static void main(String[] args)
{
double d=2.345677;
d=d*100;
System.out.println(d);
int a=(int)d;
System.out.println(a);
d=(double)a/100;
System.out.println(d);
}
}
hope this helps
Rahul
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
This is how I would do it. The code is self explanatory so I am not including any details.
class Test {
static double d = 3.14752;
public static void main (String [] args) {
Double dd = new Double(d);
String s = dd.toString();
String ss = s.substring(0,4);
System.out.println (ss);
}
}
Regards,
Milind

[This message has been edited by Milind Kulkarni (edited June 07, 2000).]
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm. Milind's solution works for the constant number given, but then so would <pre>System.out.println("3.14")</pre>.
To use this approach for arbitrary numbers is a little more complex, you have to allow for lots of digits before the point, less than 2 after the point, and a potential negative sign.
How about:

All this is interesting, but a bit pointless, when there is a built-in class to do this for us anyway. Consider:

These formatting classes were introduced to Java in version 1.1, so they should be available in your implementation.
 
Milind Kulkarni
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Frank for enlightening me !!
Regards,
Milind
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Frank,
can u tell me what are the disadvantages of my method vis-a- vis yours.

Rahul
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the only advantages of your solution are that a reader can easily see what's happening, and it would work on very old (pre 1.1) versions of Java.
The advantages of using the built-in formatting system are:
1. You don't need to write it.
2. It's extremely flexible; you can control all aspects of how your number is displayed.
3. It's internationalized; It will correctly use '.' as the decimal separator in England and the USA, but ',' in Germany (for example), so you don't need to worry about that.
4. It's available to all your classes; you don't need to worry about including a version in every applet, application, servlet, jar etc.
5. Someone else is maintaining, optimizing and testing it for you.
In short, it's always better to use a built-in version of something if it does what you want.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
thanks frank. i will start using u r method in my applications from now on.
Rahul
 
reply
    Bookmark Topic Watch Topic
  • New Topic