Solved

BigDecimal formatting without any rounding

Posted on 2006-06-16
8
1,261 Views
Last Modified: 2008-02-01
Hello All,

We have a requirement where we need to work with java.math.BigDecimal and format it in such a way so that the display would always have four decimal digits, appropriate commas and NO rounding up or down. Meaning,

Input            Output
1            1.0000
0.5            0.5000
0.0004             0.0004
0.00085            0.0008
1000            1,000.0000

We tried to format the big decimal with DecimalFormat("#,##0.0000") but there is some rounding on the decimal value i.e., if input is 0.00006 then the output is 0.0001 whereas it shoud be just 0.0000.

Any help on this would be must useful. Thanks.
0
Comment
Question by:innumonenu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16924155
0
 
LVL 5

Expert Comment

by:tbboyett
ID: 16924164
Do you need 4 decimal places always unless it has more?  Or do you want the decimals to reflect the number like:

1
.5
23.455
2.0000005
0
 

Author Comment

by:innumonenu
ID: 16924312
Like I said, we always require 4 decimal digits if the input has it or not. For example,

Input          Output
1                 1.0000
0.5              0.5000
0.0004         0.0004
0.00085       0.0008
1000            1,000.0000
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:innumonenu
ID: 16924538
DecimalFormat("#,##0.0000") almost satisfies the requirement but there is some rounding on the decimal value i.e., if input is 0.00006 then the output is 0.0001 whereas it shoud be just 0.0000.
0
 
LVL 9

Expert Comment

by:matthew016
ID: 16924579
           BigDecimal ba = new BigDecimal(1000);
            BigDecimal bb = new BigDecimal(0.5);
            BigDecimal bc = new BigDecimal(0.00006);
            
            DecimalFormat myFormat = new DecimalFormat("#,##0.0000");
            
            ba = ba.setScale(4,BigDecimal.ROUND_DOWN);
            bb = bb.setScale(4,BigDecimal.ROUND_DOWN);
            bc = bc.setScale(4,BigDecimal.ROUND_DOWN);
            
            System.out.println(myFormat.format(ba));
            System.out.println(myFormat.format(bb));
            System.out.println(myFormat.format(bc));

1.000,0000
0,5000
0,0000
0
 
LVL 9

Expert Comment

by:matthew016
ID: 16924586
(I have 1.000,0000 and not 1,000.0000 according to my Locale (fr-BE))
0
 
LVL 9

Accepted Solution

by:
matthew016 earned 125 total points
ID: 16926101
[javadoc]
Using the integer fields in this class (such as ROUND_HALF_UP) to represent rounding mode is largely obsolete; the enumeration values of the RoundingMode enum, (such as RoundingMode.HALF_UP) should be used instead.

So replace

          ba = ba.setScale(4,BigDecimal.ROUND_DOWN);
          bb = bb.setScale(4,BigDecimal.ROUND_DOWN);
          bc = bc.setScale(4,BigDecimal.ROUND_DOWN);

by

            ba = ba.setScale(4,RoundingMode.DOWN);
            bb = bb.setScale(4,RoundingMode.DOWN);
            bc = bc.setScale(4,RoundingMode.DOWN);

is same result.
0
 

Author Comment

by:innumonenu
ID: 16931420
Thanks matthew016. Not sure whether you above solution will work for input like 1.9? Will the output be 1.9000?
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 38
Eclipse for Java EE development 2 45
import as existing maven project 3 41
Java array 10 63
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question