How can I specify the number of significant digits in an output variable

조회 수: 46 (최근 30일)
Philip
Philip 2017년 9월 13일
답변: Ameer Hamza 2018년 4월 22일
I have a number, say 170803134921, in a character array. When I convert the array to a number it shows up with scientific notation.
temp = '170803134921'
str2num(temp)
ans = 1.708031349210000e+11
I would like to the number that is output in ans to display the same number of significant digits as the string did (12 digits) without the decimal point and scientific notation. How do I achieve this?

답변 (2개)

Fangjun Jiang
Fangjun Jiang 2017년 9월 13일
format LongG
str2num(temp)

Ameer Hamza
Ameer Hamza 2018년 4월 22일

You can use vpa() for arbitrary precision in your calculations. In your case

vpa(temp)

gives,

ans = 170803134921.0

You can set the precision using digits(). You will need symbolic math toolbox installed, to use these functions.

카테고리

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