Home | History | Annotate | Download | only in numfmt
      1 /********************************************************************
      2  * COPYRIGHT:
      3  * Copyright (c) 1999-2002, International Business Machines Corporation and
      4  * others. All Rights Reserved.
      5  ********************************************************************/
      6 
      7 #include "unicode/unum.h"
      8 #include "unicode/ustring.h"
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 
     12 static void uprintf(const UChar* str) {
     13     char buf[256];
     14     u_austrcpy(buf, str);
     15     printf("%s", buf);
     16 }
     17 
     18 void capi() {
     19     UNumberFormat *fmt;
     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 };
     24     UChar buf[256];
     25     int32_t needed;
     26     double a;
     27 
     28     /* Create a formatter for the US locale */
     29     fmt = unum_open(
     30           UNUM_DECIMAL,      /* style         */
     31           0,                 /* pattern       */
     32           0,                 /* patternLength */
     33           "en_US",           /* locale        */
     34           0,                 /* parseErr      */
     35           &status);
     36     if (U_FAILURE(status)) {
     37         printf("FAIL: unum_open\n");
     38         exit(1);
     39     }
     40 
     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.
     43 
     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");
     49         exit(1);
     50     }
     51 
     52     /* Show the result */
     53     printf("unum_parseDouble(\"");
     54     uprintf(str);
     55     printf("\") => %g\n", a);
     56 
     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.
     61 
     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");
     67         exit(1);
     68     }
     69 
     70     /* Show the result */
     71     printf("unum_formatDouble(%g) => \"", a);
     72     uprintf(buf);
     73     printf("\"\n");
     74 
     75     /* Release the storage used by the formatter */
     76     unum_close(fmt);
     77 }
     78