Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Converting the Amount from BigDecimal to String in Specific Format in Java

Posted on 2010-08-31
12
Medium Priority
?
615 Views
Last Modified: 2013-11-23
Could anyone please help me in writing a utility method which takes BigDecimal as input and return String as output in following format
If input is 15.91 (BigDecimal)
Output is 000000001591 ( String)
Input 115.91
Output 000000000011591
Input is 1115.91
Output is 000000111591
Input is 11115.91
Output is 000001111591
The output is 12 digit value(it contains Zeros +Dollar Amount +cents)
The entire thing represents an amount , last two digits represents cents
As the amount gets increased the number of zeros gets reduced
There is some performance tests going on I need this as soon as possible, help is appreciated
0
Comment
Question by:sree101
[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
  • 4
  • 3
  • 2
  • +2
12 Comments
 

Author Comment

by:sree101
ID: 33569567
I need this Solution ASAP
0
 
LVL 4

Expert Comment

by:darkyin87
ID: 33569705
function prependZeros(float val)
{
      String s = String.valueOf(i);
      if(s.length() < 12)
      {
            int len = s.length();
            int remainingLength = 12 - len;
      }
      StringBuffer newString = null;
      while(remainingLength > 0)
      {
            newString.append(0);
      }
      newString.append(s);
}
0
 

Author Comment

by:sree101
ID: 33570448
HI darkyin87,
could you please take some values (BigDecimal input and String output)and and expalin me about the solution
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Expert Comment

by:darkyin87
ID: 33570935
public String BigDecimalToString(BigDecimal number)
{
      //Convert the BigDecimal to String
      String str = number.toString();
      
      //Find out the length of the string
      int len = str.length();
      
      //Find the number of zeros to be prepended by subtracting the length of the string from 12
      int preprendLength = 12 - len;
      
      //Create a new stringbuffer
      StringBuffer newString = new StringBuffer("");
      
      //Intialize the newString with the number of zeros you need
      while(prependLength-- > 0)
      {
            newString.append("0");
      }
      
      //Finally append the orignal string too
      newString.append(str);
      
}
0
 

Author Comment

by:sree101
ID: 33571523
HI darkyin87,
thanks for sending me the code it worked for me, i need one more help that is i am passing

String input =  1115.91 , I need the out put like this String output = 111591
I need to elimate decimal(.)
Can you help me how can i do this
0
 
LVL 4

Accepted Solution

by:
darkyin87 earned 2000 total points
ID: 33571599
private String convert(String decimalString){
String converted = "";
if (decimalString.indexOf(".") >= 0){
String firstPart = decimalString.substring(0,
decimalString.indexOf("."));
String lastPart =
decimalString.substring(decimalString.indexOf( "."));
if (lastPart.length() > 0) lastPart =
lastPart.substring(1);
converted = firstPart + lastPart;
}
return converted;
}
0
 

Author Comment

by:sree101
ID: 33571716
Hi darkyin87,
thank you very much, every thing is working for me now
0
 
LVL 3

Expert Comment

by:DEVLitehouse
ID: 33571896
Be careful with BigDecimals and calculation. Make sure that any values a BigDecimal is created with comes in as something like String. You will get floating point errors doing something like
new BigDecimal(53);
Instead use something like
new BigDecimal("53);
0
 
LVL 3

Expert Comment

by:DEVLitehouse
ID: 33571903
oops, meant
new BigDecimal("53");
0
 
LVL 3

Expert Comment

by:hazgoduk
ID: 33623149
BigDecimal nput = new Bigdecimal("15.91");

NumberFormat format = new DecimalFormat("000000000000");

format.format(new BigDecimal(input.toString().replaceAll("\\.", "")));
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 36235311
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

636 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