Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Quickest way to get 2 decimal places from BigDecimal?

2,668 views
Skip to first unread message

laredotornado

unread,
Oct 26, 2010, 11:28:27 AM10/26/10
to
Hi,

I'm using Java 1.6. For the purposes of writing the "cents" from a
BigDecimal in a special way on a JSP page, how do I extract only the
two decimal places from a BigDecimal number? For example, if my
number is

123.45123

I want to get the number "45" (the two numbers after the decimal
place) into a string. Rounding is not important. Thanks, - Dave

Arne Vajhøj

unread,
Oct 26, 2010, 11:47:09 AM10/26/10
to

Does x.reminder(BigDecimal.ONE).setScale(2) work?

Arne


markspace

unread,
Oct 26, 2010, 12:07:54 PM10/26/10
to
On 10/26/2010 8:47 AM, Arne Vajh�j wrote:
> Does x.reminder(BigDecimal.ONE).setScale(2) work?

No.

Exception in thread "main" java.lang.ArithmeticException: Rounding necessary
at java.math.BigDecimal.divideAndRound(BigDecimal.java:1435)
at java.math.BigDecimal.setScale(BigDecimal.java:2381)
at java.math.BigDecimal.setScale(BigDecimal.java:2428)
at test.BigDecimalTest.main(BigDecimalTest.java:26)
Java Result: 1

Jean-Baptiste Nizet

unread,
Oct 26, 2010, 12:29:29 PM10/26/10
to
Le 26/10/2010 17:28, laredotornado a �crit :

Use the JSTL :

<fmt:formatNumber value="${myBigDecimal}" pattern="#.00"/>

JB.

markspace

unread,
Oct 26, 2010, 12:35:07 PM10/26/10
to

I don't know the quickest JSP. I assume that Jean-Baptiste idea isn't
going to work because you want "45" not "123.45"

Here's a Java way of doing it. Might be considered evil in a JSP.

public class BigDecimalTest {

public static void main( String[] args )
{
BigDecimal test = new BigDecimal( "123.456789" );
BigDecimal oneHundred = new BigDecimal( "100" );

System.out.println( test.subtract( test.divideToIntegralValue(
BigDecimal.ONE ) ).multiply( oneHundred ).
divideToIntegralValue( BigDecimal.ONE )
.stripTrailingZeros());
}
}

Alexander S. Mentis

unread,
Oct 26, 2010, 12:47:52 PM10/26/10
to
laredotornado wrote:

Feels like a kludge, but this might work for you:

public class Main
{
public static void main(String[] args)
{
BigDecimal bd = new BigDecimal(4096.4567890);

String textBD = bd.toString();
int radixLoc = textBD.indexOf('.');
System.out.println("Cents: " + textBD.substring
(radixLoc + 1, radixLoc + 3));
}
}

Arne Vajhøj

unread,
Oct 26, 2010, 1:19:55 PM10/26/10
to

x.reminder(BigDecimal.ONE).setScale(2, BigDecimal.ROUND_HALF_UP)

Arne

markspace

unread,
Oct 26, 2010, 1:44:14 PM10/26/10
to
On 10/26/2010 10:19 AM, Arne Vajh�j wrote:

> x.reminder(BigDecimal.ONE).setScale(2, BigDecimal.ROUND_HALF_UP)


Closer. It's "remainder" of course, not "reminder". And in my test I
had a 6 after the 45, so this actually gave .46 not .45, although the OP
did say rounding was not an issue. I think you still have to get rid of
the decimal place to meet the OP's requirements though.


BigDecimal test = new BigDecimal( "123.456789" );

System.out.println( test.remainder(BigDecimal.ONE).setScale(2,
BigDecimal.ROUND_HALF_UP ).movePointRight( 2 ) );

Prints "46" and seems to be closest to what the OP is asking for. Good
job spotting that seScale has a rounding mode readily available. For
some reason, "round()" doesn't.

Lars Enderin

unread,
Oct 26, 2010, 1:48:34 PM10/26/10
to Arne Vajhøj
I am sure that the method is called "remainder".

Jean-Baptiste Nizet

unread,
Oct 27, 2010, 1:21:12 PM10/27/10
to
Le 26/10/2010 18:35, markspace a �crit :

> On 10/26/2010 8:28 AM, laredotornado wrote:
>> Hi,
>>
>> I'm using Java 1.6. For the purposes of writing the "cents" from a
>> BigDecimal in a special way on a JSP page, how do I extract only the
>> two decimal places from a BigDecimal number? For example, if my
>> number is
>>
>> 123.45123
>>
>> I want to get the number "45" (the two numbers after the decimal
>> place) into a string. Rounding is not important. Thanks, - Dave
>
> I don't know the quickest JSP. I assume that Jean-Baptiste idea isn't
> going to work because you want "45" not "123.45"
>

Oops. Indeed. I read the OP's question a bit too quickly. Sorry.

JB.

Arne Vajhøj

unread,
Nov 26, 2010, 6:16:17 PM11/26/10
to
On 26-10-2010 13:48, Lars Enderin wrote:

> 2010-10-26 19:19, Arne Vajhøj skrev:
>> On 26-10-2010 12:07, markspace wrote:
>>> On 10/26/2010 8:47 AM, Arne Vajhøj wrote:
>>>> Does x.reminder(BigDecimal.ONE).setScale(2) work?
>>>
>>> No.
>>>
>>> Exception in thread "main" java.lang.ArithmeticException: Rounding
>>> necessary
>>> at java.math.BigDecimal.divideAndRound(BigDecimal.java:1435)
>>> at java.math.BigDecimal.setScale(BigDecimal.java:2381)
>>> at java.math.BigDecimal.setScale(BigDecimal.java:2428)
>>> at test.BigDecimalTest.main(BigDecimalTest.java:26)
>>> Java Result: 1
>>
>> x.reminder(BigDecimal.ONE).setScale(2, BigDecimal.ROUND_HALF_UP)
>>
> I am sure that the method is called "remainder".

Yup.

:-)

Arne

Arne Vajhøj

unread,
Nov 26, 2010, 6:19:19 PM11/26/10
to
On 26-10-2010 13:44, markspace wrote:
> On 10/26/2010 10:19 AM, Arne Vajh�j wrote:
>> x.reminder(BigDecimal.ONE).setScale(2, BigDecimal.ROUND_HALF_UP)
>
> Closer. It's "remainder" of course, not "reminder". And in my test I had
> a 6 after the 45, so this actually gave .46 not .45, although the OP did
> say rounding was not an issue.

Where I come from ROUND_HALF_UP is the default.

> I think you still have to get rid of the
> decimal place to meet the OP's requirements though.
>
> BigDecimal test = new BigDecimal( "123.456789" );
>
> System.out.println( test.remainder(BigDecimal.ONE).setScale(2,
> BigDecimal.ROUND_HALF_UP ).movePointRight( 2 ) );
>
> Prints "46" and seems to be closest to what the OP is asking for.

Or just substring to get rid of that period.

Arne


0 new messages