[英]Leading zero in Matlab's scientific notation
In Matlab, when printing using e
such as fprintf('%10.5e\\n', pi/100)
one will get the result to be 3.14159e-02
. 在Matlab中,当使用诸如
fprintf('%10.5e\\n', pi/100)
e
进行打印时fprintf('%10.5e\\n', pi/100)
将得到结果为3.14159e-02
。 However, what if I want the number to have a leading zero such as 0.314159e-1
? 但是,如果我希望数字有一个前导零,如
0.314159e-1
怎么办? How can I manage this with Matlab? 我如何使用Matlab进行管理?
The reason I ask is that I am trying to print to a file which I need to have leading zeros. 我问的原因是我正在尝试打印到我需要有前导零的文件。 Otherwise, I would not care.
否则,我不在乎。
Thanks. 谢谢。
I don't think there is any clever way to do it: 我认为没有任何聪明的方法可以做到:
your_number = pi;
['0.' strrep(sprintf('%10.5e',your_number*10), '.', '')]
>> ans =
0.314159e+01
my solution is pretty crude but this is just to illustrate. 我的解决方案很粗糙,但这只是为了说明。 You can do it yourself with a small function that will look for the relevant strings in the number, trim it after
e
, add 0.
in the beginning and increse by 1
the exponent at the end, for example: 您可以使用一个小函数自行完成,该函数将查找数字中的相关字符串,在
e
之后修剪它,在开头添加0.
并在结尾处增加1
指数,例如:
function b=fooo(a)
b=a;
k1 = strfind(a, '.');
k2 = strfind(a, 'e-');
suffix=num2str(str2num(b(k2+1:k2+3))+1);
b(k2+1:end)=[];
b(k1)=[];
b=['0.' b suffix];
where an input like 输入就像
ans=fooo(sprintf('%10.5e\n', pi/100))
will yield the answer: 将得出答案:
ans =
0.314159e-1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.