Tips Tricks Samples

Convert Double to BigDecimal

We have been doing a bit of Web Services integration between Domino and .Net. In our situation, the .Net application was interested in receiving numbers as BigDecimal objects. To this end I did a bit of investigation and found that this is the best way to convert from Double (my preferred format) in Java:

import java.math.BigDecimal;
/**
* A set of useful static methods to do with numbers
*/
public class Numbers
{
  /**
  * Converts a Double to BigDecimal in the most efficient and accurate manner
  * BigDecimal is the preferred format for .Net
  * @param number
  * @return
  */
  public static BigDecimal getBigDecimalFromDouble(Double number)
  {
    String strNumber = number.toString();
    BigDecimal bdReturn = new BigDecimal(strNumber);
    return bdReturn;
  }
}