]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/ustrfmt.c
ICU-6.2.8.tar.gz
[apple/icu.git] / icuSources / common / ustrfmt.c
1 /*
2 **********************************************************************
3 * Copyright (C) 2001-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 */
7
8 #include "cstring.h"
9 #include "ustrfmt.h"
10 #include <stdio.h>
11
12 U_CAPI char* U_EXPORT2
13 uprv_dtostr(double value, char *buffer, int maximumDigits,UBool fixedPoint)
14 {
15 char *itrPtr = buffer + 1; /* skip '-' or a number before the decimal */
16 char *startPtr;
17
18 sprintf(buffer,"%f",value);
19
20 /* Find the decimal point.
21 Some unusal machines use a comma when the system locale changes
22 */
23 while (isalnum(*itrPtr)) {
24 itrPtr++;
25 }
26 *itrPtr = '.';
27
28 /* truncate trailing zeros, except the one after '.' */
29 startPtr = itrPtr + 1;
30 itrPtr = uprv_strchr(startPtr, 0);
31 while(--itrPtr > startPtr){
32 if(*itrPtr == '0'){
33 *itrPtr = 0;
34 }else{
35 break;
36 }
37 }
38 return buffer;
39 }
40
41 /***
42 * Fills in a UChar* string with the radix-based representation of a
43 * uint32_t number padded with zeroes to minwidth. The result
44 * will be null terminated if there is room.
45 *
46 * @param buffer UChar buffer to receive result
47 * @param capacity capacity of buffer
48 * @param i the unsigned number to be formatted
49 * @param radix the radix from 2..36
50 * @param minwidth the minimum width. If the result is narrower than
51 * this, '0's will be added on the left. Must be <=
52 * capacity.
53 * @return the length of the result, not including any terminating
54 * null
55 */
56 U_CAPI int32_t U_EXPORT2
57 uprv_itou (UChar * buffer, int32_t capacity,
58 uint32_t i, uint32_t radix, int32_t minwidth)
59 {
60 int32_t length = 0;
61 int digit;
62 int32_t j;
63 UChar temp;
64
65 do{
66 digit = (int)(i % radix);
67 buffer[length++]=(UChar)(digit<=9?(0x0030+digit):(0x0030+digit+7));
68 i=i/radix;
69 } while(i && length<capacity);
70
71 while (length < minwidth){
72 buffer[length++] = (UChar) 0x0030;/*zero padding */
73 }
74 /* null terminate the buffer */
75 if(length<capacity){
76 buffer[length] = (UChar) 0x0000;
77 }
78
79 /* Reverses the string */
80 for (j = 0; j < (length / 2); j++){
81 temp = buffer[(length-1) - j];
82 buffer[(length-1) - j] = buffer[j];
83 buffer[j] = temp;
84 }
85 return length;
86 }