sprintf value into a specific format

3 次查看(过去 30 天)
I have some values I'd like to sprintf into a specific format. It is similar to Fortran 77's F7.5 with an exponent.
Values:
x = 38178.873648
y = 895.258425
Desired output format:
x = 38.17887e+03
y = 89.52584e+01
sprintf('%.6e',x) and sprintf('%.6e',y) get me 3.817887e+04 and 8.952584e+02. Can I specify where the decimal point is in sprintf or do I have to put in a few extra lines of code?
Thanks for the help. Sorry if this is a bit of a newby question.

回答(1 个)

Walter Roberson
Walter Roberson 2017-11-20
Sorry, sprintf() cannot control the number of digits before the decimal place when an exponent is used. Also, the number of digits in the exponent is not certain to be the same between MS Windows and Mac or Linux.
You will need to write some code that analyzes the number and prints in the format you want. (Watch out for 0. Watch out for negative numbers. Watch out for nan and inf.)

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by