Solved

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

Posted on 2004-09-19
9
2,176 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 20

Expert Comment

by:Venabili
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
But this way you loose the formatter.
You should set the locale before making the formatter...
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now