Converting whole numbers to scientific notation

조회 수: 28 (최근 30일)
Sukriti Kumar
Sukriti Kumar 2022년 6월 15일
답변: Star Strider 2022년 6월 16일
Is it possible to convert a whole number to a decimal and then later to scientific notation? For example, I would like to have '0' in the following format: 0.0000e+00. I've tried using the "format short e" and "format long e" commands but they are not working.

답변 (3개)

Image Analyst
Image Analyst 2022년 6월 15일
fprintf('%4e\n', 0);
0.000000e+00

Walter Roberson
Walter Roberson 2022년 6월 15일
Not with the default display routines, no. You can use sprintf() or compose() with %.4e format to get character vector or string() objects

Star Strider
Star Strider 2022년 6월 16일
Another option —
expstr = @(x) [x(:).*10.^ceil(-log10(abs(x(:)+(x==0)))) floor(log10(abs(x(:)+(x==0))))]; % Updated: 2021 05 04
n = 0;
Result = sprintf('%.4fe%+04d', expstr(n))
Result = '0.0000e+000'
Result = sprintf('%.4fe%+03d', expstr(n))
Result = '0.0000e+00'
.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by