How can I display numbers in scientific notation but in magnitudes of 3 and to 3 significant digits?

18 ビュー (過去 30 日間)
I am trying to display rounded values <double> in a uitable and would like do so concisely. Using scientific notation is great for this, but I would like to have the scientific notation display only as powers divisible by 3. Additionally, the I would also like to display 3 significant digits.
For example:
1 = 1
10 = 10
100 = 100
1000 = 1.00e3
10000 = 10.0e3
100000 = 100e3
1000000 = 1.00e6
  4 件のコメント
Allen
Allen 2023 年 2 月 1 日
Unfortunately, shortEng only displays 4 significant digits after the decimal, though is perfectly handles the exponents. Additionally, after looking into the problem a bit more, it appears that there is also no convenient method for formatting uitable numeric displays to this detail.
Thank you both for the responses.
John D'Errico
John D'Errico 2023 年 2 月 11 日
sprintf SHOULD offer this as one of the options.

サインインしてコメントする。

回答 (1 件)

Dongyue
Dongyue 2023 年 2 月 7 日
classdef ScientificNotation
properties(Access = private)
sign;
body;
digs;
num;
end
methods
function s = ScientificNotation(num)
s.num = num;
if num< 0
s.sign = '-';
else
s.sign = '';
end
num = double(abs(num));
if num<1000
s.body = "";
s.digs = string(num);
else
tmp = 0;
while num >= 1000
tmp = tmp +3;
num = num/1000;
end
s.body = "e"+string(tmp);
str = string(round(num,3,'significant'));
if strlength(str) == 1
str = str+".00";
elseif strlength(str) == 2
str = str+".0";
elseif strlength(str) == 3
if contains(str,'.')
str = str+"0";
end
end
s.digs = str;
end
end
function disp(s)
fprintf(string(s.num)+" = %s%s%s\n",[ s.sign, s.digs,s.body]);
end
end
end
  1 件のコメント
Allen
Allen 2023 年 2 月 11 日
Thank you for your response, but unfortunately I am trying to directly format doubles and not convert to text. I always like seeing new uses for classdefs.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by