
在本节中,我们将了解如何将数字(整数或浮点或任何其他数字类型数据)转换为字符串。
逻辑非常简单。这里我们将使用 sprintf() 函数。此函数用于将某些值或行打印到字符串中,但不在控制台中。这是 printf() 和 sprintf() 之间的唯一区别。这里第一个参数是字符串缓冲区。我们想要保存数据的地方。
Perl学习手札是台湾perl高手写的一篇文章,特打包为chm版,方便大家阅读。 关于本书 1. 关于Perl 1.1 Perl的历史 1.2 Perl的概念 1.3 特色 1.4 使用Perl的环境 1.5 开始使用 Perl 1.6 你的第一个Perl程序 2. 标量变量(Scalar) 2.1 关于标量 2.1.1 数值 2.1.2 字符串 2.1.3 数字与字符串转换 2.2 使用你自己的变量 2.3 赋值 2.3.1 直接设定 2.3.2 还可以这样 2.4 运算 2.5 变量的输出/输入 2.
0
Input: User will put some numeric value say 42.26 Output: This program will return the string equivalent result of that number like "42.26"
Step 1: Take a number from the user Step 2: Create an empty string buffer to store result Step 3: Use sprintf() to convert number to string Step 4: End
实时演示
#include<stdio.h>
main() {
char str[20]; //create an empty string to store number
float number;
printf("Enter a number: ");
scanf("%f", &number);
sprintf(str, "%f", number); //make the number into string using sprintf function
printf("</p><p>You have entered: %s", str);
}Enter a number: 46.3258 You have entered: 46.325802
以上就是C程序将一个数字转换为字符串的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号