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. Multiple Overlays Want to Redraw only one or attach my own Canvas to Mapview and control when it refreshes

I have succesfully created a tiled map Overlay that grabs tiles from a
server and displays them on my map after it caches them on the SD
card. My program is going to have two modes tracking and not
tracking...This Tile Overlay I want to refresh when the User is in
tracking Mode, or in either mode when the user Zooms or pans (this is
working fine!!!)...
I want to create a second Overlay or utilize the pre mentioned
itemized overlay to show to Other objects....
An Arrow at your current location and a compass in the corner.
The Arrow is to show your location and Point towards your bearing when
Your speed is > 1, and is to show your location and your Orientation
from the sensor when you are not moving. If you are still in Tracking
mode it will continually center the map around you...if you are in Non-
Tracking mode it will let you wander off the map...but will still show
your orientation and or if speed > 1 bearing if still in
view...Basically a look around mode.

My problem is that in continually refreshing the arrow it also forces
all the tiles to refresh Even though they are stored on the SD card
this is ridiculous...making any calls to the ItemizedOverlay or any
Scroll 0,0 or AnimateTo call Automatically wipes out the canvas and
requires a redraw of all my tiles, Also redrawing the Arrow and
Compass from the sensor also has the same effect...which means I'm
constantly redrawing tiles wether i need to or not.

Is there away to apply another canvas ontop of my map view? that won't
refresh unless i tell it to? Iv'e a couple ways.

Code: Select all
Bitmap bitmapr = Bitmap.createBitmap(mapView.getWidth()/2,
mapView.getHeight()/2, Bitmap.Config.ARGB_8888);
Canvas GARB = new Canvas(bitmapr);
GARB.drawCircle(30, 30, 30, paint);
GARB.drawLine(0, 0, mapView.getWidth()/2, mapView.getHeight()/2,
     itemizedOverlay.draw(GARB, mapView, false);
Come to think of it I'd rather have control of Two canvasas ontop of
the map and Just use the Overlay draw method to send information to my
own classes that determine if a redraw of my canvas's are necessary.
But none of the above methods work.

I'm also interested in taking it to the next level when i get this
figured out.
To orient the map based on bearing(speed>1)/Orientation(speed <1)...
...and to allow user to zoom further in than the API allows.

PS Why does The Google Maps App have to more zoom levels on the map
(no Sat) than the API Mapcontrol Allows in my area?flipper44

Posts: 3
Joined: Thu Apr 22, 2010 3:03 pm
Location: Appleton, WI
Private message


2. Regarding Toolchain

Hi All,

I am very new in Android and i want to compile android toolchain .
can some one guide from where i can download android toolchain and
supported library.



3. getting x and y coorinates in onclick event

4. SOLD: HTC Hero Coklat Metalik (mulus)

5. ServiceTestCase does not notify Observers and does not call onStartCommand

6. Questions to ask at Google I/O

7. JUnit Puzzle: How do I test a Service and an Application together?