I have problems with num2str format
I get a number from an API:
8.1784e+17
I need the exact number as str output. For instance I know it is:
'817835078876192769'.
I tried with:
num2str(x,'%u')
however this gives me the following wrong result:
ans =
'817835078876192768'
Anyone have a clue what can be wrong here?

 採用された回答

Jan
Jan 2021 年 3 月 6 日
編集済み: Jan 2021 年 3 月 6 日

0 投票

What does "I get a number from an API: 8.1784e+17" mean? Is this a char vector, string or a UINT64 or double scalar? In the last case, remember that doubles contain about 16 digits only. So there is no chance to store a value with 18 digitis accurately.
x = uint64(817835078876192769)
% uint64 817835078876192769
double(x)
% 8.17835078876193e+17
uint64(double(x))
% uint64 817835078876192768
num2str(x)
% '817835078876192769'
sprintf('%d', x)
% '817835078876192769'
sprintf('%.0f', x)
% '817835078876192768'
So the problem might be, that the value is provided as double value. You need to store the value as UINT64 or as char array or string.

2 件のコメント

Martin
Martin 2021 年 3 月 6 日
Its a double scalar yes. So there is nothing to be done in this case you think?
Jan
Jan 2021 年 3 月 6 日
It depends on what "from API" means. From which API?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

製品

リリース

R2018a

タグ

質問済み:

2021 年 3 月 6 日

コメント済み:

Jan
2021 年 3 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by