Format Number (exponential notation)

I have a code and a result is the variabile: T= 5.6789e-05
I want use a function for change the exponential notation from e-05 to e-6 and print the value in a graph.
Correct value T= 56.789e-06
Help me
sprintf('time T= %3.3e\n',T)

Answers (1)

You will not be able to do this directly. sprintf and related do not offer any control over where the decimal is put.
You can use log10 to probe the power of 10 for the value, such as -5. Divide by that power minus 1 to get a value with two leading decimals. Format that. Add on sprintf('e%d') the power minus 1

5 Comments

thankyou Walter for your fast answer.
interesting metode but I don't understand what I have to write ;)
If I have a general variable T=A*10^(x)
where A and x are unknows
(for example T=5.6789e-05 or T=458.45e-04 ...)
I want probe the power of 10 with log10
From math we know:
log10(T) = log10( A*10^(x) ) = log10(A)+log10( 10^(x) ) = log10(A)+log10(10^(x))
log10(T)=log10(A)+x
and x=log10(T)-log10(A)
or x=log10(T/A)
x(the power of 10) is different to only log10(T)
in other way
log10(5.6789e-05) is not -5
Maybe you have other ideas
thankyou
X=5.6789e-05; D=floor(log10(5.6789e-05))-1; X/10^D
gives 56.789.
really thankyou.
I didn't know floor comand, I m a student and I'm writting my thesis
Now in Command Windows (display) I obtain correct result (56.789e-06)
But in workspace the variable is again (5.6789e-05) and when I report the result in a graph legend it remain 5.6789e-05.
Do you have any ideas?
Part of my code is:
%EXAMPLE WITH t2
x=floor(log10(t2))-1
t2=t2/10^(-6-x)
legend([c b m g v ], { [sprintf('tA-O1 = 0.5* T = Ti= %3.3e\n', Ti)], ...
[sprintf('tB-tA = T= %3.3e\n', T) ], ...
[sprintf('front time T/0.6 = T1 = %3.3e\n', T1)], ...
[sprintf('time to half value t2= %3.3e\n', t2)], ...
[sprintf('peak value Vpp= %.4g\n', Vpp)], });
X=5.6789e-05;
D=floor(log10(5.6789e-05))-1;
t2S = sprintf('time to half time t2=%.4fe%d', X/10^D, D) ;
and use the variable in constructing the legend.
I have change anything.....and work fine ;)
GOOD!!! Really thankyou
Now Italy is not a good place. 6000 dead in two weeks. Take care and rest at home.

Sign in to comment.

Asked:

on 23 Mar 2020

Commented:

on 23 Mar 2020

Community Treasure Hunt

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

Start Hunting!