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

Posted on 2004-09-19
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?
Question by:cuesta
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
  • 6
  • 2
LVL 20

Expert Comment

ID: 12095187
use DecimalFormat:

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

Expert Comment

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
LVL 20

Expert Comment

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 ;)
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

LVL 20

Expert Comment

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? ;)

Author Comment

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?
LVL 20

Accepted Solution

Venabili earned 500 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
LVL 20

Expert Comment

ID: 12095558
But this way you loose the formatter.
You should set the locale before making the formatter...
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.

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

623 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