nayutaco / ptarmigan

Lightning Network (BOLT)
Apache License 2.0
132 stars 19 forks source link

bitcoind: round JSON amount value #1601

Closed nayuta-ueno closed 5 years ago

nayuta-ueno commented 5 years ago

JSON-RPC(listunspent) return BTC amount by string. jansson library use strtod() to convert double value to integer. After convert, the value need round off.

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <inttypes.h>

#define VALUES      "20999991.99978481"

int main(void)
{
    double val = strtod(VALUES, NULL);
    printf("%20.8lf\n", val);
    printf("  %20.10lf\n", val);

    // NG "2099999199978480"
    printf("%" PRIu64 "\n", (uint64_t)(val * (uint64_t)100000000));
    // OK "2099999199978481"
    printf("%" PRIu64 "\n", (uint64_t)(val * (uint64_t)100000000 + 0.5));
    return 0;
}