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 longx = -3.141592653589793;a = 287;x_new = sprintf('%.15f',x);x_new(end-2:end) = (sprintf('%d',a));x_new = str2double(x_new) Related SolutionsMATLAB: How to retrieve last 3 digits from each entry of a matrix of order 3 x 4 solution: A = [35679, 35678, 35677, 35676; 35675, 35674, 35673, 35672; 35671, 35670, 35669, 35668];last3digits=A-round(A,-3);last3digits(last3digits<0)=last3digits(last3digits<0)+1000;disp(last3digits) MATLAB: Left digits left3 = @(x)str2double(x(1:3));left3(num2str(1234))Perhaps?Or so it doesn't error on numbers < 100left3 = @(x)str2double(x(1:min(3,length(x)))); Related QuestionHow to avoid the loss of the last digit (it gives a zero instead of a one)Matching digitsThree-digit formatWhere do all these digits come fromSum of DigitsMedian of n*nHow to retrieve first 4 digits after decimal point
Best Answer