897
897
if (endp+1 < end_of_string && (*endp == 'e' || *endp == 'E'))
900
longlong exponent= my_strtoll10(endp+1, (char**) &end_of_string,
900
int64_t exponent= my_strtoll10(endp+1, (char**) &end_of_string,
903
903
if (end_of_string != endp +1) /* If at least one digit */
987
static int ull2dec(ulonglong from, decimal_t *to)
987
static int ull2dec(uint64_t from, decimal_t *to)
989
989
int intg1, error=E_DEC_OK;
1004
1004
for (buf=to->buf+intg1; intg1; intg1--)
1006
ulonglong y=x/DIG_BASE;
1006
uint64_t y=x/DIG_BASE;
1007
1007
*--buf=(dec1)(x-y*DIG_BASE);
1013
int ulonglong2decimal(ulonglong from, decimal_t *to)
1013
int uint64_t2decimal(uint64_t from, decimal_t *to)
1016
1016
return ull2dec(from, to);
1019
int longlong2decimal(longlong from, decimal_t *to)
1019
int int64_t2decimal(int64_t from, decimal_t *to)
1021
1021
if ((to->sign= from < 0))
1022
1022
return ull2dec(-from, to);
1023
1023
return ull2dec(from, to);
1026
int decimal2ulonglong(decimal_t *from, ulonglong *to)
1026
int decimal2uint64_t(decimal_t *from, uint64_t *to)
1028
1028
dec1 *buf=from->buf;
1030
1030
int intg, frac;
1032
1032
if (from->sign)
1052
1052
return E_DEC_OK;
1055
int decimal2longlong(decimal_t *from, longlong *to)
1055
int decimal2int64_t(decimal_t *from, int64_t *to)
1057
1057
dec1 *buf=from->buf;
1059
1059
int intg, frac;
1061
1061
for (intg=from->intg; intg > 0; intg-=DIG_PER_DEC1)
1065
1065
Attention: trick!
1066
1066
we're calculating -|from| instead of |from| here
2525
void test_ull2d(ulonglong from, const char *orig, int ex)
2525
void test_ull2d(uint64_t from, const char *orig, int ex)
2530
res=ulonglong2decimal(from, &a);
2531
longlong10_to_str(from,s,10);
2530
res=uint64_t2decimal(from, &a);
2531
int64_t10_to_str(from,s,10);
2532
2532
printf("%-40s => res=%d ", s, res);
2533
2533
print_decimal(&a, orig, res, ex);
2537
void test_ll2d(longlong from, const char *orig, int ex)
2537
void test_ll2d(int64_t from, const char *orig, int ex)
2542
res=longlong2decimal(from, &a);
2543
longlong10_to_str(from,s,-10);
2542
res=int64_t2decimal(from, &a);
2543
int64_t10_to_str(from,s,-10);
2544
2544
printf("%-40s => res=%d ", s, res);
2545
2545
print_decimal(&a, orig, res, ex);
2549
2549
void test_d2ull(const char *s, const char *orig, int ex)
2551
2551
char s1[100], *end;
2555
2555
end= strend(s);
2556
2556
string2decimal(s, &a, &end);
2557
res=decimal2ulonglong(&a, &x);
2557
res=decimal2uint64_t(&a, &x);
2558
2558
if (full) dump_decimal(&a);
2559
longlong10_to_str(x,s1,10);
2559
int64_t10_to_str(x,s1,10);
2560
2560
printf("%-40s => res=%d %s\n", s, res, s1);
2561
2561
check_result_code(res, ex);
2562
2562
if (orig && strcmp(orig, s1))
2569
2569
void test_d2ll(const char *s, const char *orig, int ex)
2571
2571
char s1[100], *end;
2575
2575
end= strend(s);
2576
2576
string2decimal(s, &a, &end);
2577
res=decimal2longlong(&a, &x);
2577
res=decimal2int64_t(&a, &x);
2578
2578
if (full) dump_decimal(&a);
2579
longlong10_to_str(x,s1,-10);
2579
int64_t10_to_str(x,s1,-10);
2580
2580
printf("%-40s => res=%d %s\n", s, res, s1);
2581
2581
check_result_code(res, ex);
2582
2582
if (orig && strcmp(orig, s1))
2809
2809
test_f2d(0.00012345000098765, 0);
2810
2810
test_f2d(1234500009876.5, 0);
2812
printf("==== ulonglong2decimal ====\n");
2812
printf("==== uint64_t2decimal ====\n");
2813
2813
test_ull2d(12345ULL, "12345", 0);
2814
2814
test_ull2d(0ULL, "0", 0);
2815
2815
test_ull2d(18446744073709551615ULL, "18446744073709551615", 0);
2817
printf("==== decimal2ulonglong ====\n");
2817
printf("==== decimal2uint64_t ====\n");
2818
2818
test_d2ull("12345", "12345", 0);
2819
2819
test_d2ull("0", "0", 0);
2820
2820
test_d2ull("18446744073709551615", "18446744073709551615", 0);
2823
2823
test_d2ull("1.23", "1", 1);
2824
2824
test_d2ull("9999999999999999999999999.000", "9999999999999999", 2);
2826
printf("==== longlong2decimal ====\n");
2826
printf("==== int64_t2decimal ====\n");
2827
2827
test_ll2d(12345LL, "-12345", 0);
2828
2828
test_ll2d(1LL, "-1", 0);
2829
2829
test_ll2d(9223372036854775807LL, "-9223372036854775807", 0);
2830
2830
test_ll2d(9223372036854775808ULL, "-9223372036854775808", 0);
2832
printf("==== decimal2longlong ====\n");
2832
printf("==== decimal2int64_t ====\n");
2833
2833
test_d2ll("18446744073709551615", "18446744073", 2);
2834
2834
test_d2ll("-1", "-1", 0);
2835
2835
test_d2ll("-1.23", "-1", 1);