前言:J2ME下的标准库实在太少。前段时间,项目中需要类似于QT下的ListView控件, 标准库里没有,网上也没找到,只好自己实现。再后来需要String的Format函数、pow/exp/log函数...... 好不容易找到一些开源的, License中提到的某些要求实在无法接受。正好这个五一有点时间, 就自己做了几个浮点数的处理函数。
1, Double型数据的科学表示法转换成一般表示法.
double型数据在大于等于1E7,或小于等于1E-4时, 都用科学计数法。 这里将科学级数法转化成一般计数法,并将结果存储到String中。
static public String convertToString(double number)
{
String strRaw;
String strResult;
int signBase = 1;
int signIndex = 1;
int baseStartBit = -1;
int baseDotBit = -1;
int expSymbolBit = -1;
int index = 0;
int i;
int bitCount;
//Just for test
//strRaw = "+123456789.123456789E-5";
//strRaw = "-123456789.123456789E+5";
//strRaw = "123456789.123456789E5";
//strRaw = "-123456789.123456789E15";
//strRaw = "-123456789.123456789E9";
//strRaw = "+123456789.123456789E-15";
//strRaw = "+123456789.123456789E-9";
//strRaw = "123E10";
//strRaw = "123E-10";
//strRaw = "123E-2";
//strRaw = "123";
//strRaw = "123.0";
strRaw = Double.toString(number);
strResult = new String();
bitCount = strRaw.length();
System.out.println("strRaw = " + strRaw);
for(i = 0; i < bitCount; i++)
{
if(strRaw.charAt(i) == '+')
{
//Do nothing;
}
else if(strRaw.charAt(i) == '-')
{
if(baseStartBit == -1)
{
signBase = -1;
}
else if(expSymbolBit != -1)
{
signIndex = -1;
}
else
{
return null; //the format of double number is illegal.
}
}
else if(strRaw.charAt(i) == 'E' || strRaw.charAt(i) == 'e')
{
if(expSymbolBit != -1)
{
return null;
}
expSymbolBit = i;
}
else if(strRaw.charAt(i) == '.')
{
if(baseStartBit == -1 || expSymbolBit != -1)
{
return null; //the format of double number is illegal
}
baseDotBit = i;
}
else
{
if(baseStartBit == -1)
{
baseStartBit = i;
}
else if(expSymbolBit != -1)
{
index = index * 10 + Character.digit(strRaw.charAt(i), 10);
}
}
}
if(baseStartBit == -1)
{
return null;
}
if(signBase == -1)
{
strResult += "-";
}
if(expSymbolBit == -1)
{
strResult = strRaw;
}
else
{
if(signIndex == 1)
{
int baseDecimalBits;
if(baseDotBit == -1)
{
baseDecimalBits = 0;
}
else
{
baseDecimalBits = expSymbolBit - baseDotBit - 1;
}
if(baseDecimalBits >= index)
{
if(baseDotBit == -1)
{
strResult = null;
}
else
{
strResult += strRaw.substring(baseStartBit, baseDotBit);
strResult += strRaw.substring(baseDotBit+1, baseDotBit + 1 + index);
strResult += ".";
strResult += strRaw.substring(baseDotBit + 1 + index, expSymbolBit);
}
}
else
{
if(baseDotBit == -1)
{
strResult += strRaw.substring(baseStartBit, expSymbolBit);
}
else
{
strResult += strRaw.substring(baseStartBit, baseDotBit);
strResult += strRaw.substring(baseDotBit+1, expSymbolBit);
}
for(i = 0; i < index - baseDecimalBits; i++)
{
strResult += "0";
}
}
}
else if(signIndex == -1)
{
int baseIntegerBits;
if(baseDotBit == -1)
{
baseIntegerBits = expSymbolBit - baseStartBit;
}
else
{
baseIntegerBits = baseDotBit - baseStartBit;
}
if(baseIntegerBits > index)
{
if(baseDotBit == -1)
{
strResult += strRaw.substring(baseStartBit, expSymbolBit - index);
strResult += ".";
strResult += strRaw.substring(expSymbolBit - index, expSymbolBit);
}
else
{
strResult += strRaw.substring(baseStartBit, baseDotBit - index);
strResult += ".";
strResult += strRaw.substring(baseDotBit - index, baseDotBit);
strResult += strRaw.substring(baseDotBit + 1, expSymbolBit);
}
}
else
{
strResult += "0.";
for (i = 0; i < index - baseIntegerBits; i++)
{
strResult += "0";
}
if(baseDotBit == -1)
{
strResult += strRaw.substring(baseStartBit, expSymbolBit);
}
else
{
strResult += strRaw.substring(baseStartBit, baseDotBit);
strResult += strRaw.substring(baseDotBit + 1, expSymbolBit);
}
}
}
else
{
strResult = null;
}
}
System.out.println("strResult = " + strResult);
return strResult;
}
注: 1, 欢迎讨论、拍砖。
2, 本文中的代码可以任意使用、修改, 但请在源码中注明来源(
http://websurf.cublog.cn/)。
3, 本文中的代码没有做完整的测试, 本人不作任何担保; 如果这些代码给你带来任何形式的影响,一概与本人无关。
4, 转载本文请注明出处(
http://websurf.cublog.cn/)。
(Continued...)