Problems of convert decimal number to a string

Now I want to convert a input decimal number (maybe a input number, not known) to a string, I try to use num2str and string function, but there are some problems that I can not solve.
num = [110.00001, 10.00000000000000001,99.01]';
% I want to num become ["110.00001", "10.00000000000000001","99.01"]
num2str([110.00001, 10.00000000000000001,99.01])
By use num2str it will delete some decimal places, I don't know how to do it, holp someone would help me.

Antworten (3)

Ajay Kumar
Ajay Kumar am 28 Jun. 2020

0 Stimmen

You can define precision property in num2str. For example:
num = [110.00001, 10.00000000000000001,99.01]';
your_ans = num2str(num,10);

6 Kommentare

madhan ravi
madhan ravi am 28 Jun. 2020
"I want to num become ["110.00001", "10.00000000000000001","99.01"]"
zongxian
zongxian am 28 Jun. 2020
The result is ['110.00001', ' 10', ' 99.01']
Not ["110.00001", "10.00000000000000001","99.01"]
Ajay Kumar
Ajay Kumar am 28 Jun. 2020
Bearbeitet: Ajay Kumar am 28 Jun. 2020
Numeric variable style long has fixed-decimal format with 15 digits after the decimal point for double values. To deal with more than 15 decimal places you need to have some high precision library such as symbolic math toolbox.
double is not fixed-decimal format with 16 digits after the decimal point: double is floating point format with 53 effective bits, which happens to work out as between 15 and 16 relative digits. You can reliably get 15 relative digits, but you cannot reliably get 16 digits; it depends on the exact range of values.
zongxian
zongxian am 28 Jun. 2020
Bearbeitet: zongxian am 28 Jun. 2020
I have an idea, if we set the decimal number as num2str(10.0000000001, '%10.20f'), and then delete the zeros one by one (from the last one digits), until the non-zeros digit, now I have try it, it show '10.00000000010000000827', why the last digits are not zeros??
The exact value stored for 10.0000000001 is
10.0000000001000000082740370999090373516082763671875
Values down to 10.000000000000001 can be distinguished from 10.0
10.0000000000000017763568394002504646778106689453125
It is important to remember for this purpose that double() does not use decimal representation: it uses IEEE 754 Double Precision Binary representation, which is a 64 bit representation with 1 sign bit, 11 exponent bits, and 52 explicit fraction bits, plus one "hidden" fraction bit (it is hidden in the exponent in a way.)

Melden Sie sich an, um zu kommentieren.

KSSV
KSSV am 28 Jun. 2020
Bearbeitet: KSSV am 28 Jun. 2020

0 Stimmen

iwant = sprintf("0.12f 0.12f 0.12f",num) ;
Try with sprintf.
You can split each number into a cell using num2cell, and use cellfun with sprintf. Or run a loop for each num.

1 Kommentar

zongxian
zongxian am 28 Jun. 2020
This way need give the format of the number, but if we don't know the detail decimal number (i.e. '0.12f') will return wrong answer.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 28 Jun. 2020

Kommentiert:

am 8 Okt. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by