Solved

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

Posted on 2004-09-19
9
2,210 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
[X]
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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

752 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