Last active
August 29, 2015 14:11
-
-
Save quentinadam/051acf585f27f4db00b8 to your computer and use it in GitHub Desktop.
Print gcc decimal128
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int sprint(char* str, _Decimal128 value) { | |
long long high = value; | |
unsigned long long low; | |
if (value > 0) { | |
value = value * 10000000000000000000.0DL; | |
low = value - high * 10000000000000000000.0DL; | |
} else { | |
value = value * -10000000000000000000.0DL; | |
low = value + high * 10000000000000000000.0DL; | |
} | |
if (low > 0) { | |
int n = sprintf(str, "%lld.%019llu", high, low); | |
int i = 0; | |
while (i < 18 && str[n - i - 1] == '0') i++; | |
str[n - i] = 0; | |
return n - i; | |
} else { | |
return sprintf(str, "%lld", high); | |
} | |
} | |
int main() { | |
char buffer[40]; | |
sprint(buffer, -12345678901234.035DL); | |
printf("%s\n", buf); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment