sprintf value into a specific format

3 ビュー (過去 30 日間)
Charlie Wood
Charlie Wood 2017 年 11 月 20 日
回答済み: Walter Roberson 2017 年 11 月 20 日
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 Center および File ExchangeNumeric Types についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by