Format printing of floats?

by BobG » Sat, 06 Mar 2010 23:18:53 GMT

Sponsored Links
 What shows: 1.23456789
What I want: 1.23
In c it would be printf("%4.2f",n);
In Java its? (I dont know yet).


Format printing of floats?

by Kitzy » Sun, 07 Mar 2010 23:57:46 GMT

 Look up DecimalFormat
You'll need to create the format on that object:
String pattern = "0.##"; // or "0.00" depending if you want 1.5 to
show a 1.5 or 1.50
DecimalFormat my_formatter = new DecimalFormat (pattern);
String formatted_string = my_formatter.format (1.23456789)

You could put the pattern directly into the constructor to only take 2
lines of code instead of 3.



Sponsored Links

Format printing of floats?

by Scott Marlowe » Wed, 10 Mar 2010 04:32:50 GMT

 I don't know about the Java printf equivalent, but this function will
return a double with only the specified number of decimal places. You
should be able to modify with a "return bd.toString" or simplify in
some other way to satisfy your needs.

private static double round(double d, int decimalPlace)
        BigDecimal bd = new BigDecimal(Double.toString(d));
        bd = bd.setScale(decimalPlace,BigDecimal.ROUND_HALF_UP);
        return bd.doubleValue();


Other Threads

1. FaceDetector class

Hi, I want to use the FaceDetector class, which also has a pose
function for the headpose, but it seems that the head pose is not
implemented because it always returns 0.
It does return the distance between the eyes, and the point between
the two eyes, so im guessing that at some point in the face detection,
the coordinates of the eyes are known. So i'm now checking out the
source code to see if i can output the eye coordinates myself, but it
seems that the actual face detection is done by calling a seperate
function called "ff_detect" for which i cannot find any code.

Do you guys know how i can get this code, or how i can get the head
pose otherwise?

Thanks very much


2. Failed binder transaction when passing byte[] (from camera) between intents

Binary data works upto about 100KByte. Anything larger, you very
likely get that error.

For images in RGB_565, this is about 277 x 184 pixels max (277 x 184 *
2 = 101936)


3. how to read a String into an EditText window?

4. Using ContentResolver to add a contact in the mobile

5. How to pass a String like input of a Activity

6. Sample Programs

7. Lots of questions and no answers: upgrading <my device> from Android 1.6 to 2.0