How do I output a 64-bit unsigned integer as numbers without scientific notation?

14 次查看(过去 30 天)
I have an application that determines the datatype of an array element as numeric (float), integer, character, and logical. When it encounters an integer datatype, I want to output it as numbers and not in scientific notation. The integer may be an unsignedLong 64-bit integer.
  2 个评论
Stephen23
Stephen23 2016-9-27
编辑:Stephen23 2016-9-27
I have never seen integer variables displayed with scientific notation (displayed in the command window, excluding any explicit string formatting commands such as sprintf, fprintf, num2str, etc). In fact the format documentation clearly states that integers are always displayed at full precision: "MATLAB always displays integer data types to the appropriate number of digits for the data type. For example, MATLAB uses 3 digits to display int8 data types (for instance, -128:127). Setting the output format to short or long does not affect the display of integer-type variables."
Lest try this with some real examples:
>> format long
>> intmax('uint64')
ans =
18446744073709551615
>> format short
>> intmax('uint64')
ans =
18446744073709551615
>> format shortE
>> intmax('uint64')
ans =
18446744073709551615
>> format shortG
>> intmax('uint64')
ans =
18446744073709551615
>> format bank
>> intmax('uint64')
ans =
18446744073709551615
None of them display integers with scientific notation. Can you please show us an example of how you get MATLAB to display an integer number with scientific notation.
Guillaume
Guillaume 2016-9-27
So the conclusion is that the only way for the integer to appear in scientific notation is that it is actually stored as double in matlab.
Of course, if the number was originally an (u)int64 then there is a real risk that some precision will have be lost. Any integer above
uint64(flintmax)
may not be represented accurately if stored as double.

请先登录,再进行评论。

回答(3 个)

José-Luis
José-Luis 2016-9-27
编辑:José-Luis 2016-9-27
I don't get it. If you define your value to be or if it actually is uint64 then Matlab displays all digits by default. Just try:
x = uint64(2^64 - 1)
And it should display:
18446744073709551615

Steven Lord
Steven Lord 2016-9-27
编辑:Steven Lord 2016-9-27
You can cause a uint64 value to be displayed in scientific notation if you use sprintf or fprintf with a format specification intended for use with floating-point numbers.
U = intmax('uint64')
sprintf('%e\n', U)
sprintf('%g\n', U)
To display an unsigned integer without using scientific notation you should use %u, %o, %x, or %X as listed in the formatSpec table on the sprintf documentation page.
sprintf('%u\n', U)

KSSV
KSSV 2016-9-27

类别

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