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

46 Ansichten (letzte 30 Tage)
Philip
Philip am 13 Sep. 2017
Beantwortet: Ameer Hamza am 22 Apr. 2018
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?

Antworten (2)

Fangjun Jiang
Fangjun Jiang am 13 Sep. 2017
format LongG
str2num(temp)

Ameer Hamza
Ameer Hamza am 22 Apr. 2018

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.

Community Treasure Hunt

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

Start Hunting!

Translated by