Quickest way to get 2 decimal places from BigDecimal?

L

laredotornado

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
 
A

Arne Vajhøj

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

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

Arne
 
M

markspace

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
 
J

Jean-Baptiste Nizet

Le 26/10/2010 17:28, laredotornado a écrit :
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

Use the JSTL :

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

JB.
 
M

markspace

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"

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());
}
}
 
A

Alexander S. Mentis

laredotornado said:
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

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));
}
}
 
A

Arne Vajhøj

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)

Arne
 
M

markspace

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.
 
L

Lars Enderin

2010-10-26 19:19, Arne Vajhøj skrev:
x.reminder(BigDecimal.ONE).setScale(2, BigDecimal.ROUND_HALF_UP)
I am sure that the method is called "remainder".
 
J

Jean-Baptiste Nizet

Le 26/10/2010 18:35, markspace a écrit :
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.
 
A

Arne Vajhøj

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top