【问题标题】:Leading zero in Matlab's scientific notationMatlab科学计数法中的前导零
【发布时间】:2013-05-08 23:20:23
【问题描述】:

在 Matlab 中,当使用e 打印时,例如fprintf('%10.5e\n', pi/100) 会得到3.14159e-02 的结果。但是,如果我希望数字有前导零,例如0.314159e-1,该怎么办?如何使用 Matlab 进行管理?

我问的原因是我正在尝试打印到需要前导零的文件。否则,我不会在意。

谢谢。

【问题讨论】:

    标签: matlab file-io scientific-notation


    【解决方案1】:

    我认为没有什么聪明的方法可以做到:

    your_number = pi;
    ['0.' strrep(sprintf('%10.5e',your_number*10), '.', '')]
    
    >> ans =
    
    0.314159e+01
    

    【讨论】:

      【解决方案2】:

      我的解决方案非常粗略,但这只是为了说明。你可以自己用一个小函数来查找数字中的相关字符串,在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];
      

      这样的输入
      ans=fooo(sprintf('%10.5e\n', pi/100))
      

      会给出答案:

      ans =
         0.314159e-1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-08
        • 1970-01-01
        • 1970-01-01
        • 2012-12-14
        • 2014-05-26
        相关资源
        最近更新 更多