필터 지우기
필터 지우기

sprintf value into a specific format

조회 수: 2 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by