Solved

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

Posted on 2004-09-19
9
2,193 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 30

Expert Comment

by:Mayank S
ID: 12095405
0
 
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 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
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checkbox and ListView in Android Layout 4 65
simple java question 3 55
Cipher Configuration on Apache HTTPD 4 62
servlet example 11 40
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

776 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