ICU-994 fixed uprv_digitsAfterDecimal()

X-SVN-Rev: 4997
This commit is contained in:
George Rhoten 2001-06-14 00:29:38 +00:00
parent 17efe6e7c0
commit fec6a9409a
2 changed files with 17 additions and 19 deletions

View File

@ -627,23 +627,19 @@ int32_t
uprv_digitsAfterDecimal(double x)
{
char buffer[20];
int32_t numDigits;
char *p;
int32_t numDigits, bytesWritten;
char *p = buffer;
int32_t ptPos, exponent;
/* negative numbers throw off the calculations*/
x = fabs(x);
/* cheat and use the string-format routine to get a string representation*/
/* (it handles mathematical inaccuracy better than we can), then find out */
/* many characters are to the right of the decimal point */
sprintf(buffer, "%.9g", x);
p = uprv_strchr(buffer, '.');
if (p == 0)
return 0;
bytesWritten = sprintf(buffer, "%+.9g", x);
while (isdigit(*(++p))) {
}
ptPos = (int16_t)(p - buffer);
numDigits = (int16_t)(strlen(buffer) - ptPos - 1);
ptPos = (int32_t)(p - buffer);
numDigits = (int32_t)(bytesWritten - ptPos - 1);
/* if the number's string representation is in scientific notation, find */
/* the exponent and take it into account*/
@ -651,8 +647,8 @@ uprv_digitsAfterDecimal(double x)
p = uprv_strchr(buffer, 'e');
if (p != 0) {
int16_t expPos = (int16_t)(p - buffer);
numDigits -= strlen(buffer) - expPos;
exponent = (int16_t)(atoi(p + 1));
numDigits -= bytesWritten - expPos;
exponent = (int32_t)(atoi(p + 1));
}
/* the string representation may still have spurious decimal digits in it, */
@ -664,6 +660,9 @@ uprv_digitsAfterDecimal(double x)
--numDigits;
}
numDigits -= exponent;
if (numDigits < 0) {
return 0;
}
return numDigits;
}

View File

@ -39,7 +39,6 @@ addPUtilTest(TestNode** root)
static void TestPUtilAPI(){
double n1=0.0, y1=0.0, expn1, expy1;
int result=0;
double value1 = 0.021;
int32_t hi=0;
uint32_t lo=0;
@ -129,17 +128,17 @@ static void TestPUtilAPI(){
}
log_verbose("Testing the API uprv_digitsAfterDecimal()....\n");
result=uprv_digitsAfterDecimal(value1);
doAssert(result, 3, "uprv_digitsAfterDecimal() failed.");
doAssert(uprv_digitsAfterDecimal(value1), 3, "uprv_digitsAfterDecimal() failed.");
doAssert(uprv_digitsAfterDecimal(1.2345E2), 2, "uprv_digitsAfterDecimal(1.2345E2) failed.");
doAssert(uprv_digitsAfterDecimal(1.2345E-2), 6, "uprv_digitsAfterDecimal(1.2345E-2) failed.");
doAssert(uprv_digitsAfterDecimal(1.2345E2), 2, "uprv_digitsAfterDecimal(1.2345E2) failed.");
doAssert(uprv_digitsAfterDecimal(-1.2345E-20), 24, "uprv_digitsAfterDecimal(1.2345E-20) failed.");
doAssert(uprv_digitsAfterDecimal(1.2345E20), 0, "uprv_digitsAfterDecimal(1.2345E20) failed.");
doAssert(uprv_digitsAfterDecimal(-0.021), 3, "uprv_digitsAfterDecimal(-0.021) failed.");
doAssert(uprv_digitsAfterDecimal(23.0), 0, "uprv_digitsAfterDecimal(23.0) failed.");
doAssert(uprv_digitsAfterDecimal(0.022223333321), 9, "uprv_digitsAfterDecimal(0.022223333321) failed.");
log_verbose("Testing the API u_versionToString().....\n");
u_versionToString(versionArray, versionString);
if(strcmp(versionString, "1.0.2.2") != 0){