Link to home
Start Free TrialLog in
Avatar of sree101
sree101

asked on

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

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
Avatar of sree101
sree101

ASKER

I need this Solution ASAP
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);
}
Avatar of sree101

ASKER

HI darkyin87,
could you please take some values (BigDecimal input and String output)and and expalin me about the solution
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);
      
}
Avatar of sree101

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of darkyin87
darkyin87

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sree101

ASKER

Hi darkyin87,
thank you very much, every thing is working for me now
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);
oops, meant
new BigDecimal("53");
BigDecimal nput = new Bigdecimal("15.91");

NumberFormat format = new DecimalFormat("000000000000");

format.format(new BigDecimal(input.toString().replaceAll("\\.", "")));
Avatar of Mike McCracken
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.