Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- int main(void) {
- char *str = (char *) malloc(sizeof(char *));
- int offset = 0;
- double num = 123456.00001;
- int num_int = (int) num;
- double decimal = num - num_int;
- int digits = 0;
- if (num < 0) {
- str[offset++] = '-';
- }
- while (num_int) {
- digits++;
- num_int /= 10;
- }
- for (int i = digits - 1; i >= 0; i--) {
- str[offset++] = '0' + (((int) (num / pow(10, i))) % 10);
- }
- if (decimal) str[offset++] = '.';
- while (decimal) {
- decimal *= 10;
- str[offset++] = '0' + (int) decimal;
- decimal = decimal - (int) decimal;
- }
- str[offset] = '\0';
- printf("num : %f\n", num);
- printf("actual_num: %s\n", str);
- return 0;
- }
Add Comment
Please, Sign In to add comment