Formats BigDecimal into a SQL floating-point literal
/**********************************************************************
Copyright (c) 2002 Mike Martin (TJDO) and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
2002 Kelly Grizzle (TJDO)
2003 Andy Jefferson - coding standards
...
**********************************************************************/
import java.math.BigDecimal;
/**
* Utility class providing SQL formatting methods.
*
* @version $Revision: 1.2 $
**/
public class SQLFormat
{
/**
* Formats the given BigDecimal value into a SQL floating-point literal.
* BigDecimal.toString() is not well suited to this purpose because it never
* uses E-notation, which causes some values with large exponents to be
* output as long strings with tons of zeroes in them.
*
* @param bd The number to format.
*
* @return The formatted String.
*/
public static String format(BigDecimal bd)
{
String digits = bd.unscaledValue().abs().toString();
int scale = bd.scale();
int len = digits.length();
/* Normalize by removing any trailing zeroes. */
while (len > 1 && digits.charAt(len - 1) == '0')
{
--scale;
--len;
}
if (len < digits.length())
{
digits = digits.substring(0, len);
}
StringBuffer sb = new StringBuffer();
if (bd.signum() < 0)
{
sb.append('-');
}
int exponent = len - scale;
if (exponent < 0 || exponent > len)
{
/* Output in E-notation. */
sb.append('.').append(digits).append('E').append(exponent);
}
else if (exponent == len)
{
/* Output as an integer. */
sb.append(digits);
}
else
{
/* Output as "intDigits.fracDigits". */
sb.append(digits.substring(0, exponent)).append('.').append(digits.substring(exponent));
}
return sb.toString();
}
}
/////////////////////////////////////////
/*
* The terms of the JPOX License are distributed with the software documentation.
*/
package org.jpox.util;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Random;
import org.jpox.util.SQLFormat;
import junit.framework.TestCase;
/**
* Tests the functionality of {@link SQLFormat}.
*
* @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
*
*/
public class SQLFormatTest extends TestCase
{
/**
* Used by the JUnit framework to construct tests. Normally, programmers
* would never explicitly use this constructor.
*
* @param name Name of the <tt>TestCase</tt>.
*/
public SQLFormatTest(String name)
{
super(name);
}
private static final String[][] CUSTOM_CASES =
{
{ "0", "0" },
{ "12.34", "12.34" },
{ "1.234", "1234e-3" },
{ ".1234E12", "123400000000" },
{ "-.1234", "-1234e-4" },
{ "1234", ".1234e+4" },
{ ".75E10", ".75e+10" },
{ "-.75E10", "-7.5e+9" },
{ ".5E-9", "5e-10" }
};
public void testCustomValues() throws Throwable
{
for (int i = 0; i < CUSTOM_CASES.length; ++i)
assertEquals(CUSTOM_CASES[i][0], SQLFormat.format(new BigDecimal(CUSTOM_CASES[i][1])));
}
private static final int NUM_RANDOM_CASES = 5000;
public void testRandomValues() throws Throwable
{
Random rnd = new Random(0L);
for (int i = 0; i < NUM_RANDOM_CASES; ++i)
{
BigDecimal bd1 = new BigDecimal(new BigInteger(128, rnd), rnd.nextInt(100));
String s = SQLFormat.format(bd1);
BigDecimal bd2 = new BigDecimal(SQLFormat.format(bd1));
assertEquals("Formatting " + bd1 + " yielded " + s + " which doesn't equal", 0, bd1.compareTo(bd2));
}
}
}
Related examples in the same category