简体   繁体   English

使用c中的printf打印常量指数值

[英]Print constant exponent value using printf in c

I used %e format specifier. 我使用%e格式说明符。 Tried %3.2e or %4.3e . 尝试%3.2e%4.3e

Example: 例:
if var=0.001342 , then I want to print 0.1342e-02 . 如果var=0.001342 ,那么我想打印0.1342e-02 But prints 1.342e-03 但打印1.342e-03
if var=0.543124 , then I want to print 54.3124e-02 . 如果var=0.543124 ,那么我想打印54.3124e-02 But prints 5.43124e-01 但打印5.43124e-01
if var=0.0123653 , then I want to print 1.23653e-02 . 如果var=0.0123653 ,那么我想打印1.23653e-02

That is what ever maybe the value, I just want to fix my exponent value an print the result. 这就是价值,我只是想修改我的指数值打印结果。

It is not possible to specify the number of digits before the decimal separator with the standard floating point format specifiers. 使用标准浮点格式说明符无法指定小数分隔符之前的位数。

It is the whole point of "scientific notation" to have one (or zero) digits before the decimal point and all the info about "where is the decimal point" comes from the exponent. 在小数点之前有一个(或零)数字是“科学记数法”的全部要点,所有关于“小数点在哪里”的信息来自指数。

If you have some fixed format (always e-2) you may produce something like: 如果您有一些固定的格式(总是e-2),您可能会产生以下内容:

printf("%f  e-2", x*100.0);

Edit 编辑

Or, to make it more standard scientific notation : 或者,使其成为更标准的科学记数法

printf("%f * 10^-3", x*1e3);

From the manual: 从手册:

eE The double argument is rounded and converted in the style [-]d.ddde+-dd where there is one digit before the decimal-point character and the number of digits after it is equal to the precision; eE双参数被舍入并以[ - ] d.ddde + -dd样式转换,其中小数点字符前面有一位数字,后面的位数等于精度; if the precision is missing, it is taken as 6; 如果精度丢失,则取6; if the precision is zero, no decimal-point character appears. 如果精度为零,则不显示小数点字符。 An E conversion uses the letter E' (rather than e') to introduce the exponent. E转换使用字母E' (rather than e')来引入指数。 The exponent always contains at least two digits; 指数始终包含至少两位数; if the value is zero, the exponent is 00. 如果值为零,则指数为00。

eE is for the scientific notation, so only one non nul digit before the decimal point. eE用于科学记数法,因此小数点前只有一个非零数字。

You need to make the conversion by yourself, like: 您需要自己进行转换,例如:

float number = 0.01023;
float normalized = number*100;
int mantissa_int_part = normalized;
int mantissa_decimal_part = (normalized-mantissa_int_part)*10000;
printf("%02d.%04d e-2",mantissa_int_part,mantissa_decimal_part);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM