fprintf format -.123456879E-02

4 views (last 30 days)
Enrico Tapavicza
Enrico Tapavicza on 10 Nov 2015
Commented: Star Strider on 11 Nov 2015
How can I use fprintf to print in scientific format, always starting with 0.XXXXXXE-0Y in case of a positive number, or -.XXXXXXE-0Y in the case of a negative number.
Example: a=-0.07639297; fprintf('%E',a);
This will print the number as: -7.639297E-02
However, I want to have it as: -.7639297E-01
Thanks!

Answers (1)

Star Strider
Star Strider on 10 Nov 2015
You have to ‘adjust’ the format a bit, but it can be done as a string:
a=-0.07639297;
expstr = @(x) [x*10.^floor(-log10(abs(x))) floor(log10(abs(x))+1)];
Result = sprintf('%.7fE%+04d', expstr(a))
Result =
-0.7639297E-001
Tweak the format in the sprintf call to your liking.
  4 Comments
Enrico Tapavicza
Enrico Tapavicza on 11 Nov 2015
All right, thank you very much!
Star Strider
Star Strider on 11 Nov 2015
My pleasure!
The sincerest expression of thanks here on MATLAB Answers is to Accept the Answer that most closely solves your problem.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!