Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Floating number format in Java (01.10 instead of 1.1)?

Posted on 2004-09-19
9
Medium Priority
?
2,231 Views
Last Modified: 2012-08-13
Hi, I'm writing some program results in a text file in Java, using BufferedWriter and the function "write" (salida.write("Text"+f)), f is a float, which value is (for ex. 3.8) but I want to write in the file 03.80, and in other function I have 340.00, and I want to print it exactly like that (not 340). Any ideas?
0
Comment
Question by:cuesta
  • 6
  • 2
9 Comments
 
LVL 20

Expert Comment

by:Venabili
ID: 12095187
use DecimalFormat:

NumberFormat formatter = new DecimalFormat("######.00");
String s = formatter.format(3.8);// 3.80
String s = formatter.format(340);// 340.00
0
 
LVL 20

Expert Comment

by:Venabili
ID: 12095190
If you need 1.1 to be shown as 01.10
NumberFormat formatter = new DecimalFormat("####00.00");
String s = formatter.format(1.1);// 01.10
0
 
LVL 20

Expert Comment

by:Venabili
ID: 12095195
In short :
In the format use 0 if you want the position always to be shown, #  if you want toi be shown only if exists ;)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 20

Expert Comment

by:Venabili
ID: 12095413
and how is this different from the already posted? Or better to ask, what exactly it adds as an information for this question? ;)
0
 

Author Comment

by:cuesta
ID: 12095467
Yeah! That works, thanx a lot, but, using:

NumberFormat formatter = new DecimalFormat("####00.00");
String s = formatter.format(1.1);

it prints "01,10" and I need "01.10", any way to change this?
0
 
LVL 20

Accepted Solution

by:
Venabili earned 2000 total points
ID: 12095535
It comes from the locales - in your locale the decimal point is a comma.
Try with a local with decimail point a point. Canada is such for example:
Locale locale = Locale.CANADA;
String string = NumberFormat.getNumberInstance(locale).format(1.1);  // 01.10
0
 
LVL 20

Expert Comment

by:Venabili
ID: 12095558
But this way you loose the formatter.
You should set the locale before making the formatter...
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 12099312
>> and how is this different from the already posted?

Nothing, just a complete listing ;-) even contains for stuff like negative numbers, so on and so forth.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

916 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