c - double 到 char* 无需任何外部方法

标签 c arrays

我正在尝试将 C 中的 double 字转换为 char* 数组,但我无法使用 sprintf 或 stdlib 库之外的任何方法。我正在尝试采用一种数学方法,找到位数+点号并将每个数字放入数组中,但是当精度很高时(例如 102.45555),这真的很困难

关于如何实现这一目标有什么想法吗?

最佳答案

您可以采用这种方法:

cast the double to an int, then store the digits of the int in the char array
append the decimal point
subtract the int from the double
while you don't have sufficient number of digits after decimal point:
    multiply the double by 10
    cast it to int and append it to the array
    subtract the int from the double

关于c - double 到 char* 无需任何外部方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22351660/

相关文章:

C 和类型转换 - 为什么这个程序在 6 处停止?

将 C 数组和指针代码转换为 Go

c - 将 pragma 扩展为注释(对于 doxygen)

c++ - 分配具有相同值的多维 std::array 的简单方法?

c++ - 具有不同类型的CUDA每线程阵列

javascript - 如何在 Javascript/ES6 中获得这样的输出

c - 警告 : implicit declaration of function GTK2

c - 加载应用程序时出现访问冲突

php - 我不知道如何使用 "sort"数组

arrays - 在 python 中找到长度为 n 的子序列的最小总和的最快方法