How to truncate all the decimal places??

24 次查看(过去 30 天)
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 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by