How to truncate all the decimal places??

조회 수: 41 (최근 30일)
Phoebe
Phoebe 2014년 2월 25일
댓글: Walter Roberson 2021년 8월 16일
I am working on a code that loops over time and have been told that I am at this time "rounding the time to the nearest whole number. This is wrong. You need to truncate all the decimal places." I am unsure exactly what this means and how to implement so any would be appreciated. A relevant extract of my code is below;
time=0;
dt=0.01; %Define time step
tfinal=40;
diagstep=10; %diagnostic time step
diagcounter=0;
while time<tfinal
time=time+dt;
if (round(time/diagstep * diagstep) == time)
[xcounter] = ParticleCounterFun(x0, particleno)
diagcounter=diagcounter+1
end
end
%%xcounter function file is;
function [ xcounter ] = ParticleCounterFun( x0, particleno )
%Initialise counters used in E field diagnostics
counter01=0;
counter12=0;
counter23=0;
counter34=0;
counter45=0;
for np=1:particleno
if (0<=x0(np) & x0(np)<1)
counter01=counter01+1;
end
if (1<=x0(np) & x0(np)<2)
counter12=counter12+1;
end
if (2<=x0(np) & x0(np)<3)
counter23=counter23+1;
end
if (3<=x0(np) & x0(np)<4)
counter34=counter34+1;
end
if (4<=x0(np) & x0(np)<5)
counter45=counter45+1;
end
end
xcounter=[counter01 counter12 counter23 counter34 counter45]
end
It is in the condition for the IF statement that i must 'truncate all decimal places' and not just round to nearest whole number so if anyone could help me on this!
Thanks!
Phoebe

답변 (3개)

Iain
Iain 2014년 2월 25일
fix(time) rounds time towards 0, so it gets rid of the decimal places. (truncates numerically)
str = num2str(time); truncated = str(1); this takes the 1st character when the number is expressed as a string (truncates textually - you will need to change this to do it properly)
It looks like you wanted to round to something other than the nearest integer, but got it wrong: If you want rid of all but the 1st decimal place, then you want, fix(time*10)/10 if you want rounded down to the nearest 10, fix(time/10)*10.

Dishant Arora
Dishant Arora 2014년 2월 25일
doc cast
cast your variable to int16/32/64 depending upon your requirement.
  댓글 수: 2
Phoebe
Phoebe 2014년 2월 26일
what effect does changing int8 to int16 or int32 please? If i try the following;
if ((int8(time/diagstep) * diagstep) == time)
[xcounter] = ParticleCounterFun(x0, particleno)
diagcounter=diagcounter+1
end
It still does not work??
Dishant Arora
Dishant Arora 2014년 2월 27일
what is it that you exactly want to do?? probably you need to change diagnostic step to 100.
round(time*100)/100==dt
or use fix as Iain said.

댓글을 달려면 로그인하십시오.


Gabor
Gabor 2021년 8월 16일
편집: Gabor 2021년 8월 16일
fix(1.12) -> remove decimals -> result: 1
fix(23.858457) -> remove decimals -> result: 23
For datetime comparison:
datenum(datetime('now','Format','d-MMM-y')) -> 738385.03
fix(datenum(datetime('now','Format','d-MMM-y'))) -> 738385.00
Took me 20 minutes to find the answer to this simple question, I hope I saved some time for you!
  댓글 수: 1
Walter Roberson
Walter Roberson 2021년 8월 16일
t = datetime('now')
t = datetime
16-Aug-2021 23:34:27
dateshift(t, 'start', 'hour')
ans = datetime
16-Aug-2021 23:00:00

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by