Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

To set precision for float in JApplet  RSS feed

 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created JApplet and I use label to do some calculations according to slider's value and to show it. The result is double type which is written in format like this 2,5666668E-4. I would like to write it like simple decimal number with 2 decimals. How to do that?

Greetings!
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what the java.text.NumberFormat class does.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't know about it. Can you explain me how to use it?
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot set a precision for a float; it is always 24 bits. You can, however, display different formats of numbers with methods like String#format(), Formatter#format(), PrintStream#format() and PrintStream#printf(), which all work the same way; I believe they all call one of the methods of Formatter. The full details are in the documentation for Formatter, and there is a shorter introduction here in the Java™ Tutorials.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thank you, I'll take a look.
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read this, it looks similar to C, but I need to apply this to applet. How to do that? I don't need to print anything, I need to display label value that will be well formated... Any ideas...?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue really has nothing to do with applets, or any kind of GUI, actually - it's simply a matter of formatting a float (or double) as a String. You can find examples at http://www.exampledepot.com/egs/java.text/pkg.html under "Numbers". (You may want to go through the examples under "Dates" as well - you're bound to need those at some point in the future.)
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maja neskovic wrote: . . . it looks similar to C, . . .
Yes, it does look similar to the <stdio.h> printf( . . . ) function in C.

As Ulf says, it is a case of creating a String in the correct format somehow, which you can display on yuor Applet/Panel/system.out/whatever.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have looked this, but I don't understand how to apply this to my problem. What to do with variable s which does some formatting? How can I merge it with my valy/300000 which returns exponential notation, actually how to put it in this:

P.S. I used "" to make it become string so I don't have problems with number formats
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used "" to make it become string so I don't have problems with number formats

That's a crude way of converting a double to a string, and it creates exactly the problem you're trying to avoid.

Have you looked at the examples I mentioned?
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I looked, but like I said, I'm a bit confused about putting that in my applet, I mean in my code. Where to put it in JLabel?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you would use that code to convert the double to a String - then you can use that String as a parameter to JLabel.setText. It'll be more than one line of code.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Succeed!!!

Thank you, Ulf, very much
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!