1 /********************************************************************
3 * Copyright (c) 1999-2002, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/unum.h"
8 #include "unicode/ustring.h"
12 static void uprintf(const UChar* str) {
20 UErrorCode status = U_ZERO_ERROR;
21 /* The string "987654321.123" as UChars */
22 UChar str[] = { 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33,
23 0x32, 0x31, 0x30, 0x2E, 0x31, 0x32, 0x33, 0 };
28 /* Create a formatter for the US locale */
30 UNUM_DECIMAL, /* style */
32 0, /* patternLength */
36 if (U_FAILURE(status)) {
37 printf("FAIL: unum_open\n");
41 /* Use the formatter to parse a number. When using the C API,
42 we have to specify whether we want a double or a long in advance.
44 We pass in NULL for the position pointer in order to get the
45 default behavior which is to parse from the start. */
46 a = unum_parseDouble(fmt, str, u_strlen(str), NULL, &status);
47 if (U_FAILURE(status)) {
48 printf("FAIL: unum_parseDouble\n");
53 printf("unum_parseDouble(\"");
55 printf("\") => %g\n", a);
57 /* Use the formatter to format the same number back into a string
58 in the US locale. The return value is the buffer size needed.
59 We're pretty sure we have enough space, but in a production
60 application one would check this value.
62 We pass in NULL for the UFieldPosition pointer because we don't
63 care to receive that data. */
64 needed = unum_formatDouble(fmt, a, buf, 256, NULL, &status);
65 if (U_FAILURE(status)) {
66 printf("FAIL: format_parseDouble\n");
71 printf("unum_formatDouble(%g) => \"", a);
75 /* Release the storage used by the formatter */