English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

like when you have 1.2 it will print 1.2
1.0 will print just 1
1.23 will print 1.23
1.3333 will round to 2 decimals --> 1.33
basically if it has less than 2 decimal it will not add zero, but when it has more than 2 decimals it will round to two decimal

if yes, please specify
thanks

2007-02-11 02:46:08 · 3 answers · asked by dan 2 in Computers & Internet Programming & Design

3 answers

Use the NumberFormat class

public String setDigits(float f)
{
NumberFormat numberformat = NumberFormat.getInstance();
numberformat.setMinimumFractionDigits(2);
return numberformat.format(f);
}

2007-02-18 06:01:04 · answer #1 · answered by theguy 2 · 2 0

Yes, look at the String Formatting methods, the syntax is very similar to C tokens.

2007-02-11 11:43:25 · answer #2 · answered by Anonymous · 0 1

What Railgun means is, look at the java.lang.String class. It has a static "format" method:
String.format(String format, Object input ...)

Not sure why someone gave Railgun a negative vote. He was correct, just not very informative.

2007-02-15 12:46:11 · answer #3 · answered by vincentgl 5 · 0 1

fedest.com, questions and answers