How to convert a string (numbers) to number(double) without scientific notation

7 ビュー (過去 30 日間)
Sunil Kunjachan
Sunil Kunjachan 2017 年 3 月 3 日
コメント済み: Walter Roberson 2018 年 2 月 19 日
suppose
x='12344566787665546789988776556';
then
y=str2num(x)
should be as
y=12344566787665546789988776556
class(y) should be double
  2 件のコメント
Sunil Kunjachan
Sunil Kunjachan 2017 年 3 月 3 日
Thanks Walter. Here my problem is :- Adding two such huge string of numbers and the result should be as a new string with no decimal points inside
Stephen23
Stephen23 2017 年 3 月 3 日
編集済み: Stephen23 2017 年 3 月 3 日
@Sunil Kunjachan: we understand what you want to do, and Walter Roberson explained why you cannot use double or int64 for this and also gave you two ways to make it work. What else do you need?

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

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 3 月 3 日
No, that is not possible.
You can use
sprintf('%.0f', 12344566787665546789988776556)
but the result you will get back will be
12344566787665547646753308672
This reflects the fact that your values require 94 bits to represent exactly, but double can hold a maximum of 53 bits of precision.
Your values are also too large to represent in uint64.
If you need exact integers that large then you will need to use the Symbolic Toolbox, or a package such as John D'Errico's Variable Precision Integers in the File Exchange.
  2 件のコメント
Walter Roberson
Walter Roberson 2017 年 3 月 3 日
For the case of adding two long numbers, then you can read the numbers as strings, and break the string up into a series of decimal digits, and then do "long-hand" addition on the two. Start at the last (least significant), add the two, if the result is greater than 10 subtract 10 and carry 1, move on to the next pair, add the two plus the carry, and so on.

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by