Skip to content
Myr

example5.c

This file

Path

vendor/decNumber/example5.c

36 lines · 1.5 KiB · C

Last changed by

Not known.

History of this file

Take it

1/* ------------------------------------------------------------------ */
2/* Decimal Number Library Demonstration program                       */
3/* ------------------------------------------------------------------ */
4/* Copyright (c) IBM Corporation, 2001, 2007.  All rights reserved.   */
5/* ----------------------------------------------------------------+- */
6/*                                                 right margin -->|  */
7
8// example5.c -- decimal64 conversions
9
10#include "decimal64.h"             // decimal64 and decNumber library
11#include <stdio.h>                 // for (s)printf
12
13int main(int argc, char *argv[]) {
14  decimal64 a;                     // working decimal64 number
15  decNumber d;                     // working number
16  decContext set;                  // working context
17  char string[DECIMAL64_String];   // number->string buffer
18  char hexes[25];                  // decimal64->hex buffer
19  int i;                           // counter
20
21  if (argc<2) {                    // not enough words
22    printf("Please supply a number.\n");
23    return 1;
24    }
25  decContextDefault(&set, DEC_INIT_DECIMAL64); // initialize
26
27  decimal64FromString(&a, argv[1], &set);
28  // lay out the decimal64 as eight hexadecimal pairs
29  for (i=0; i<8; i++) {
30    sprintf(&hexes[i*3], "%02x ", a.bytes[i]);
31    }
32  decimal64ToNumber(&a, &d);
33  decNumberToString(&d, string);
34  printf("%s => %s=> %s\n", argv[1], hexes, string);
35  return 0;
36  } // main