• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /********************************************************************
2  *   © 2016 and later: Unicode, Inc. and others.
3  *   License & terms of use: http://www.unicode.org/copyright.html
4  *************************************************************************
5  *************************************************************************
6  * COPYRIGHT:
7  * Copyright (c) 1999-2002, International Business Machines Corporation and
8  * others. All Rights Reserved.
9  *************************************************************************/
10 
11 #include "unicode/unum.h"
12 #include "unicode/ustring.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 
uprintf(const UChar * str)16 static void uprintf(const UChar* str) {
17     char buf[256];
18     u_austrcpy(buf, str);
19     printf("%s", buf);
20 }
21 
capi()22 void capi() {
23     UNumberFormat *fmt;
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 };
28     UChar buf[256];
29     int32_t needed;
30     double a;
31 
32     /* Create a formatter for the US locale */
33     fmt = unum_open(
34           UNUM_DECIMAL,      /* style         */
35           0,                 /* pattern       */
36           0,                 /* patternLength */
37           "en_US",           /* locale        */
38           0,                 /* parseErr      */
39           &status);
40     if (U_FAILURE(status)) {
41         printf("FAIL: unum_open\n");
42         exit(1);
43     }
44 
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.
47 
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");
53         exit(1);
54     }
55 
56     /* Show the result */
57     printf("unum_parseDouble(\"");
58     uprintf(str);
59     printf("\") => %g\n", a);
60 
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.
65 
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");
71         exit(1);
72     }
73 
74     /* Show the result */
75     printf("unum_formatDouble(%g) => \"", a);
76     uprintf(buf);
77     printf("\"\n");
78 
79     /* Release the storage used by the formatter */
80     unum_close(fmt);
81 }
82