d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Only Display Decimals? > Decimalformat Maybe?
12Next
Add Reply New Topic New Poll
Member
Posts: 13,042
Joined: Apr 12 2008
Gold: 0.00
Oct 14 2014 06:36pm
Okay, I just need to be able to display ONLY the decimal on a double number, and it must display every digit.

I thought about using DecimalFormat but I don't think it will allow me to display every digit without displaying zeros on some.

Example:
Input:
3.42
17.0
47.151

Code

DecimalFormat df = new DecimalFormat("?????.???");


I would have to find a way to take the whole integer portion of each double, and subtract it from the original
Printing doubles without DecimalFormat will do what I want about (display each decimal digit, while not showing zeros)
So, how would I go about assigning maybe the integer whole portion of each double to a int variable, and subtracting it.
Then, multiplying the decimal by 100 to change it to a whole number.
Keep in mind, I will not know what the input will be.

Correct Output:
42
0
151

Member
Posts: 13,042
Joined: Apr 12 2008
Gold: 0.00
Oct 14 2014 06:50pm
Here's what I've got thus far in order to only get the decimal:

double num = 3.42;
int num2 = 3;

double total = (num-num2);

BUT

when I do this, it assigns .4199999999993 to total...
Why does it do this?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 14 2014 06:56pm
Quote (DD_Rocking @ Oct 14 2014 08:50pm)
Here's what I've got thus far in order to only get the decimal:

double num = 3.42;
int num2 = 3;

double total = (num-num2);

BUT

when I do this, it assigns .4199999999993 to total...
Why does it do this?


i wouldn't recommend trying to handle it yourself. keep in mind doubles are stored in ieee format, which is binary.

http://en.wikipedia.org/wiki/Double-precision_floating-point_format
Member
Posts: 13,042
Joined: Apr 12 2008
Gold: 0.00
Oct 14 2014 07:07pm
Quote (carteblanche @ Oct 14 2014 07:56pm)
i wouldn't recommend trying to handle it yourself. keep in mind doubles are stored in ieee format, which is binary.

http://en.wikipedia.org/wiki/Double-precision_floating-point_format


How would I go about doing this in a different method? My knowledge of java is pretty limited thus far lol...
Member
Posts: 6,562
Joined: Oct 29 2007
Gold: 4.00
Oct 14 2014 07:27pm
maybe use mod? e: actually I think you'll have the same precision issues

This post was edited by Aimed_Shot on Oct 14 2014 07:28pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 14 2014 07:31pm
i would double check the decimal format. otherwise, cast to string, grab what's right of the decimal.
Member
Posts: 13,042
Joined: Apr 12 2008
Gold: 0.00
Oct 14 2014 08:05pm
I posted on stackoverflow and i was directed to using BigDecimal... Problem is, i've never used BigDecimal, and I really don't see how to use it..
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 14 2014 08:09pm
Quote (DD_Rocking @ Oct 14 2014 10:05pm)
I posted on stackoverflow and i was directed to using BigDecimal... Problem is, i've never used BigDecimal, and I really don't see how to use it..


http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial
Member
Posts: 13,042
Joined: Apr 12 2008
Gold: 0.00
Oct 14 2014 08:27pm
Quote (carteblanche @ Oct 14 2014 09:09pm)


Thanks for the help. Out of some miracle, I tried using "#" in DecimalFormat

It worked as it rounds properly. I'll try other numbers

Problem is, 99.99 gives 100, as it rounds up to 100.

I'll check out that BigDecimal

This post was edited by DD_Rocking on Oct 14 2014 08:28pm
Member
Posts: 13,042
Joined: Apr 12 2008
Gold: 0.00
Oct 14 2014 08:48pm
I'm like going cross-eyed looking at this for some reason.. I hate to ask for spoon feeding, but I literally don't know what to do...

What would I use in order to use it with my example?

Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll