How to truncate all the decimal places??
    32 views (last 30 days)
  
       Show older comments
    
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
0 Comments
Answers (3)
  Iain
      
 on 25 Feb 2014
         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.
0 Comments
  Dishant Arora
      
 on 25 Feb 2014
        doc cast
cast your variable to int16/32/64 depending upon your requirement.
2 Comments
  Dishant Arora
      
 on 27 Feb 2014
				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
      
 on 16 Aug 2021
        
      Edited: Gabor
      
 on 16 Aug 2021
  
      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 Comment
  Walter Roberson
      
      
 on 16 Aug 2021
				t = datetime('now')
dateshift(t, 'start', 'hour')
See Also
Categories
				Find more on Logical in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



