Click here to Skip to main content
13,045,285 members (65,387 online)
Rate this:
Please Sign up or sign in to vote.
Hi there,
I have a whole number counting up and I would like to put a decimal in it to make it look like a US Dollar amount counting up. so the number 59 would be .59 and the number 259 would be 2.59.

I tried using the decimal method like this (00.##) but that just adds to 0's on the end of the number like this, 59 would be 59.00.

Any help would be awesome!

Thank you
Posted 12-Jan-13 4:42am
Justin Jones 12-Jan-13 11:32am
I tried this
float pump1time = Math.abs(timerTextP1 / 100);
timerTextP1Pump1.setText(pump1time + "");

and it looks like its trying to work but it shows 0.0 till the count up equals 1 then it shows 1.0.
it won't show the decimal as it counts up. Weird.
sarathsprakash 12-Jan-13 11:36am
do you only want to have the two digits to be followed after the precision
like if it is 25678 then it should be 256.78 or 25.678
Justin Jones 12-Jan-13 11:56am
only 2 digits after decimal like a US dollar amount.
25678 would be 256.78
thank you
Richard MacCutchan 12-Jan-13 12:36pm
See my suggestion below.
Justin Jones 12-Jan-13 23:01pm
I tried your way, but it changes nothing.

private int timerTextP1;
private int dollars = timerTextP1 / 100;
private int cents = timerTextP1 % 100;

System.out.format("timerTextP1 = %d.%d", dollars, cents);
timerTextP1Pump1.setText(timerTextP1 + "");

am I doing that right? it just displays the number normal
Richard MacCutchan 13-Jan-13 5:13am
Well you need to use some other format command to set the converted string into the timerTextP1 field. It's no good using System.out.format as that just writes to the console. I am not sure what the actual method would be in Android code.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I'd just divide by 100 before displaying it.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You could try something like:
int amount;
int dollars = amount / 100;
int cents = amount % 100;
System.out.format("Amount = %d.%d", dollars, cents);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 12 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100