]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/numfmt/capi.c
1 /********************************************************************
2 * © 2016 and later: Unicode, Inc. and others.
3 * License & terms of use: http://www.unicode.org/copyright.html#License
4 *************************************************************************
5 *************************************************************************
7 * Copyright (c) 1999-2002, International Business Machines Corporation and
8 * others. All Rights Reserved.
9 *************************************************************************/
11 #include "unicode/unum.h"
12 #include "unicode/ustring.h"
16 static void uprintf(const UChar
* str
) {
24 UErrorCode status
= U_ZERO_ERROR
;
25 /* The string "987654321.123" as UChars */
26 UChar str
[] = { 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33,
27 0x32, 0x31, 0x30, 0x2E, 0x31, 0x32, 0x33, 0 };
32 /* Create a formatter for the US locale */
34 UNUM_DECIMAL
, /* style */
36 0, /* patternLength */
40 if (U_FAILURE(status
)) {
41 printf("FAIL: unum_open\n");
45 /* Use the formatter to parse a number. When using the C API,
46 we have to specify whether we want a double or a long in advance.
48 We pass in NULL for the position pointer in order to get the
49 default behavior which is to parse from the start. */
50 a
= unum_parseDouble(fmt
, str
, u_strlen(str
), NULL
, &status
);
51 if (U_FAILURE(status
)) {
52 printf("FAIL: unum_parseDouble\n");
57 printf("unum_parseDouble(\"");
59 printf("\") => %g\n", a
);
61 /* Use the formatter to format the same number back into a string
62 in the US locale. The return value is the buffer size needed.
63 We're pretty sure we have enough space, but in a production
64 application one would check this value.
66 We pass in NULL for the UFieldPosition pointer because we don't
67 care to receive that data. */
68 needed
= unum_formatDouble(fmt
, a
, buf
, 256, NULL
, &status
);
69 if (U_FAILURE(status
)) {
70 printf("FAIL: format_parseDouble\n");
75 printf("unum_formatDouble(%g) => \"", a
);
79 /* Release the storage used by the formatter */