How can I convert douvle value to int or Integer?

조회 수: 13 (최근 30일)
Mira le
Mira le 2020년 2월 12일
답변: Steven Lord 2020년 2월 12일
during the coding my programe I 've got a double value
example:
num= 1.1110e+03
I 'm going to use this value in other value but I should convert it in int
How can I do that ?
please help me
THanks

답변 (4개)

Steven Lord
Steven Lord 2020년 2월 12일
Do you need to convert to an integer data type or is converting to an integer value (if it's not one already) sufficient?
If you need an integer value check that it's not one already first. This could involve changing the default display format
format longg
or involve checking that when you round it you get the same number back.
x = 1234567;
x == round(x)
If it's not an integer value, use one of the rounding functions (round, floor, ceil, fix) to round it to the nearest integer value.
If you do need an integer data type use one of the integer data type conversion functions on the page to which Shivaraj Durairaj linked. KSSV and stozaki listed some of those conversion functions. One other conversion function that none of the previous posters mentioned was cast which is different from typecast. cast preserves the value (which likely means the bit pattern used to store the data in memory may change) while typecast preserves the bit pattern used to store the data in memory (which likely means the values will change.)

KSSV
KSSV 2020년 2월 12일
Read about int32, int64, uint32, uint64.

Shivaraj Durairaj
Shivaraj Durairaj 2020년 2월 12일
To convert from double to the desired integer type, refer the following link.

stozaki
stozaki 2020년 2월 12일
If you convert from double to int32, you can use int32 function.
It work casting as int32.
intnum = int32(num);

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by