Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a Decimal to an Integer in Matlab or Simulink. (Not Rounding)

So this maybe an easy question. I have some incoming decimal .12345 and I want to make that 12345. Now this value can change so I dont want to just multiply it by 100,000. For example if the incoming value was .123 I want 123, If it was .3219 I want 3219.

like image 591
XIGRIMxREAPERIX Avatar asked Mar 06 '23 21:03

XIGRIMxREAPERIX


1 Answers

here's something to play with: (assuming your input is a number (double or single)

  f=@(x) x*10^sum(floor(x*10.^[0:16])~=x*10.^[0:16])

this assumes a floating point accuracy of up to 16 digits...

try f(.123) ...

an additional note, the output will look like an integer but it really is still a double\single. In order to really make it to an integer class one you'll need to cast with uint64(f(x)) or whatever integer type you need.

Another solution is to cast to string using num2str, take out the 0. and cast back to num using str2num:

 f=@(x) str2num(subsref(num2str(x),struct('type','()','subs',{{1,3:numel(num2str(x))}})));

ugly but works...

like image 96
bla Avatar answered Apr 07 '23 08:04

bla