How do I set the exponential size in fprintf?

조회 수: 66 (최근 30일)
MJackP
MJackP 2015년 8월 5일
댓글: Star Strider 2015년 8월 5일
I'm trying to get the fprintf command to output specifically with the e6 scientific notation size. Is this possible?
The function is:
function [ dv_km3 dv_m3 ] = r2dv( r )
%r2dv % this script converts radius values for a sphere (m)
% to volume (km3 and m3)
format long
dv_m3 = (4/3)*pi*r^3
% fprintf('dv_m3 = %d.e\n',dv_m3)
dv_km3 = dv_m3/(10^9)
% fprintf('dv_km3 = %d\n',dv_km3)
end
I would like the dv_m3 to output always as x10^6 (10e6)

채택된 답변

Star Strider
Star Strider 2015년 8월 5일
One possibility:
x = rand(1,10) * 1E8;
xe6 = sprintf('%.3fE+6\n', x./1E6);
Experiment to get the result you want.
  댓글 수: 2
MJackP
MJackP 2015년 8월 5일
Perfect, thanks for the mega-fast reply!
For anyone else using this, my code incorporating his answer is:
function [ dv_km3 dv_m3 ] = r2dv( r )
%r2dv % this script converts radius values for a sphere (m)
% to volume (km3 and m3)
dv_m3 = (4/3)*pi*r^3;
fprintf('dv_m3 = %.3fe+6\n',dv_m3./1E6)
dv_km3 = dv_m3/(10^9);
fprintf('dv_km3 = %.3f\n',dv_km3)
end
Star Strider
Star Strider 2015년 8월 5일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by