2001-08-17 01:40:34 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2005-08-23 18:44:52 +00:00
|
|
|
* Copyright (C) 2001-2005, International Business Machines
|
2001-08-17 01:40:34 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
2001-11-30 00:06:13 +00:00
|
|
|
#include "cstring.h"
|
2001-08-17 01:40:34 +00:00
|
|
|
#include "ustrfmt.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include <stdio.h>
|
2001-08-17 01:40:34 +00:00
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI char* U_EXPORT2
|
2005-08-23 18:44:52 +00:00
|
|
|
uprv_dtostr(double value, char *buffer, int32_t maximumDigits,UBool fixedPoint)
|
2001-08-17 01:40:34 +00:00
|
|
|
{
|
2001-08-29 19:10:52 +00:00
|
|
|
char *itrPtr = buffer + 1; /* skip '-' or a number before the decimal */
|
|
|
|
char *startPtr;
|
|
|
|
|
|
|
|
sprintf(buffer,"%f",value);
|
|
|
|
|
|
|
|
/* Find the decimal point.
|
|
|
|
Some unusal machines use a comma when the system locale changes
|
|
|
|
*/
|
|
|
|
while (isalnum(*itrPtr)) {
|
|
|
|
itrPtr++;
|
|
|
|
}
|
|
|
|
*itrPtr = '.';
|
|
|
|
|
|
|
|
/* truncate trailing zeros, except the one after '.' */
|
|
|
|
startPtr = itrPtr + 1;
|
|
|
|
itrPtr = uprv_strchr(startPtr, 0);
|
|
|
|
while(--itrPtr > startPtr){
|
|
|
|
if(*itrPtr == '0'){
|
|
|
|
*itrPtr = 0;
|
2001-08-17 01:40:34 +00:00
|
|
|
}else{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2003-01-27 17:22:27 +00:00
|
|
|
/***
|
|
|
|
* Fills in a UChar* string with the radix-based representation of a
|
|
|
|
* uint32_t number padded with zeroes to minwidth. The result
|
|
|
|
* will be null terminated if there is room.
|
|
|
|
*
|
|
|
|
* @param buffer UChar buffer to receive result
|
|
|
|
* @param capacity capacity of buffer
|
|
|
|
* @param i the unsigned number to be formatted
|
|
|
|
* @param radix the radix from 2..36
|
|
|
|
* @param minwidth the minimum width. If the result is narrower than
|
|
|
|
* this, '0's will be added on the left. Must be <=
|
|
|
|
* capacity.
|
|
|
|
* @return the length of the result, not including any terminating
|
|
|
|
* null
|
2001-08-17 01:40:34 +00:00
|
|
|
*/
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2003-01-27 17:22:27 +00:00
|
|
|
uprv_itou (UChar * buffer, int32_t capacity,
|
|
|
|
uint32_t i, uint32_t radix, int32_t minwidth)
|
2001-08-17 01:40:34 +00:00
|
|
|
{
|
|
|
|
int32_t length = 0;
|
|
|
|
int digit;
|
|
|
|
int32_t j;
|
|
|
|
UChar temp;
|
|
|
|
|
|
|
|
do{
|
|
|
|
digit = (int)(i % radix);
|
|
|
|
buffer[length++]=(UChar)(digit<=9?(0x0030+digit):(0x0030+digit+7));
|
|
|
|
i=i/radix;
|
2003-01-27 17:22:27 +00:00
|
|
|
} while(i && length<capacity);
|
2001-08-17 01:40:34 +00:00
|
|
|
|
2003-01-27 17:22:27 +00:00
|
|
|
while (length < minwidth){
|
2001-08-17 01:40:34 +00:00
|
|
|
buffer[length++] = (UChar) 0x0030;/*zero padding */
|
|
|
|
}
|
|
|
|
/* null terminate the buffer */
|
2003-01-27 17:22:27 +00:00
|
|
|
if(length<capacity){
|
2001-08-17 01:40:34 +00:00
|
|
|
buffer[length] = (UChar) 0x0000;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reverses the string */
|
2003-01-27 17:22:27 +00:00
|
|
|
for (j = 0; j < (length / 2); j++){
|
2001-08-17 01:40:34 +00:00
|
|
|
temp = buffer[(length-1) - j];
|
|
|
|
buffer[(length-1) - j] = buffer[j];
|
|
|
|
buffer[j] = temp;
|
|
|
|
}
|
|
|
|
return length;
|
2001-08-17 02:53:35 +00:00
|
|
|
}
|