当前位置:网站首页>C语言刷题 | 用%f控制符输出6位小数(17)
C语言刷题 | 用%f控制符输出6位小数(17)
2022-06-22 09:10:00 【C语言小火车】
目录
例17:C语言实现用%f输出实数,且只能得到6位小数
解题思路:
不指定输出数据的长度,由系统根据数据的实际情况决定数据所占的列数。系统处理的方法一般是:实数中的整数部分全部输出,小数部分输出6位。
源代码演示:
#include<stdio.h>//头文件
int main() //主函数
{
double double_temp,temp;//定义双精度浮点型变量
double_temp=1.0;//初始化double_temp
temp=double_temp/3;//将double_temp除以3得到得结果赋值给temp
printf("%f\n",temp);//输出temp
return 0;//函数返回值为0
}编译结果:
0.333333
--------------------------------
Process exited after 0.074 seconds with return value 0
请按任意键继续. . .在上述代码中,虽然double_temp是双精度型,double_temp/3的结果也是双精度,但是用%f格式声明只能输出6位小数。
这里也可以用%m.nf指定数据宽度和小数位数。
在C语言中,/ 是除,如果是整数相除,只取整数部分,没有四舍五入之类的;%是取模,即取除法的余数。
如果取模发现被除数比除数小,比如:
#include<stdio.h>//头文件
int main() //主函数
{
printf("%d",2%5);
}%的结果是商为0余2 ,输出如下:
2
--------------------------------
Process exited after 1.792 seconds with return value 0
请按任意键继续. . .大白话就是在求模时,如果被除数比除数小,输出的值则是被除数本身。
边栏推荐
- 文件小能手---multer
- kali木马入侵win7系统
- .a文件链接库的使用
- 【node】node+短信api 实现验证码登录
- PHP login registration page
- Luogu p4292 [wc2010] reconstruction plan
- [target detection] | detection error mechanism why object detectors fail: investigating the influence of the dataset
- Solidity from introduction to practice (III)
- Record some Oracle operation commands
- Summary of key knowledge of induction motor in Electrical Engineering (reflected in existing topics)
猜你喜欢
![In the monorepo learning, execute NPM run build to report error[err\u require\esm] of ES module](/img/76/ec4776bcd452584290ef8bf5d0e06f.jpg)
In the monorepo learning, execute NPM run build to report error[err\u require\esm] of ES module

yolov5 export Gpu推理模型导出

【目标检测】|检测错误机制 Why Object Detectors Fail: Investigating the Influence of the Dataset

文件小能手---multer

Why can MySQL indexes improve query efficiency so much?

【详解】神经网络矩阵的点乘与叉乘(pytorch版)

Use record of rabbit nest

报告:在技术领域,男性更有可能获得工作面试的机会

Didi's two-sided summary

20 status mode
随机推荐
The dream of "getting rich overnight" is broken. Beware of virtual currency investment scams!!!
Instanceinforeplicator class of Eureka (service registration assistant)
版本问题导致“无法定位程序输入点OPENSSL_sk_new_reserve于动态链接库C:\Users...\libssl-1_1-x64.dll”
Matlab内数据及数据类型转换
The third-party libraries commonly used in golang development are not the most complete, but more complete
ffmpeg之volumedetect
新型冠状病毒疫情
景联文科技:机器学习AI数据集产品汇总(三)
电机学感应电动机重点知识总结(现有题目中反映的)
IP address (IPv4)
OpenCV每日函数 直方图相关(3)
Off line identification of least square method
Didi's two-sided summary
mknod
面试突击59:一个表中可以有多个自增列吗?
[QNX Hypervisor 2.2用户手册]5.6 关闭Guest
File upload attack and protection
性能优化专题
12 yuan sharing mode
How did the Delphi labs, the master of economic models behind axie and Luna, come into being?