Reputation: 3
I want to write code which can round up a set of data and then print it to 3 decimal places. I know that I need to use Ceil function but don't know how to write the correct code. What my code prints is 1 and 2 only! For example for number 1.2345 rounds up to 2.000. I would like to round the number to be 1.235 using ceil function.
My code is:
row = 1;
col = 1;
HIGH_ERROR = 2.5;
LOW_ERROR = 0.0;
% Read the file
rawData = dlmread('data_1.csv',',');
% Get the size
[MAX_ROWS, MAX_COLS] = size(rawData);
errorMap = double(zeros(MAX_ROWS, MAX_COLS));
value = ceil(rawData(row, col)*1000/1000);
%Print the raw data
fprintf('Raw Data\n');
for row = 1 : MAX_ROWS
for col = 1 : MAX_COLS
fprintf('%0.3f ', rawData(row, col));
end
fprintf('\n');
end
%Print the Error Map
fprintf('Error Map\n');
for row = 1 : MAX_ROWS
for col = 1 : MAX_COLS
if rawData(row, col) > HIGH_ERROR
errorMap(row, col) = rawData(row, col);
rawData(row, col) = HIGH_ERROR;
if rawData(row, col) < LOW_ERROR
errorMap(row, col) = rawData(row, col);
rawData(row, col) = LOW_ERROR;
end
end
fprintf('%0.3f ', errorMap(row, col));
end
fprintf('\n');
end
%Print the Rounded Data
fprintf('Rounded Data\n');
for row = 1 : MAX_ROWS
for col = 1 : MAX_COLS
value = ceil(rawData(row, col)*1000/1000);
fprintf('%0.3f ', value);
end
fprintf('\n');
end
Upvotes: 0
Views: 2439
Reputation: 4953
A nice trick which should work on any language which has round()
would be:
Round Up (Ceil) -> ceil(x) = round(x + 0.5)
.
Round Down (Floor) -> floor(x) = round(x - 0.5)
.
Upvotes: 0