Azzera filtri
Azzera filtri

sprintf value into a specific format

2 visualizzazioni (ultimi 30 giorni)
Charlie Wood
Charlie Wood il 20 Nov 2017
Risposto: Walter Roberson il 20 Nov 2017
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.

Risposte (1)

Walter Roberson
Walter Roberson il 20 Nov 2017
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.)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by