MATLAB: How to replace last 3 digits in a floating point number by another 3 digits

replace last digits

Let
x = -3.141592653589793;
a = 287;
then how can I replace last 3 digits of x i.e. 793 by a ?

Best Answer

format long
x = -3.141592653589793;
a = 287;
x_new = sprintf('%.15f',x);
x_new(end-2:end) = (sprintf('%d',a));
x_new = str2double(x_new)