Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Rounding Digits
Replies: 1   Last Post: May 4, 2014 5:44 PM

 Messages: [ Previous | Next ]
 Derek Goring Posts: 3,922 Registered: 12/7/04
Re: Rounding Digits
Posted: May 4, 2014 5:44 PM

On Monday, May 5, 2014 9:22:08 AM UTC+12, Kris wrote:
> Im trying to create a program that will round any number to any specific digit. I have an array of numbers in column A in an excel document, then the specific digit I want that number rounded to in column B. So, I will use xlsread to gather that information from excel to start. Then I will use xlswrite to send that information to excel in column C (answer). I have a mac so I can see if my code is working or not. I'd appreciate any help.
>
>
>
>
>
> matrix = xlsread('Round.xlsx','A');
>
> numbers = matrix(:,1);
>
> digits = matrix(:,2);
>
>
>
> normNums = numbers./digits;
>
> n = length(normNums);
>
> signNums = ones(n, 1);
>
> roundedNormNums = zeros(n, 1);
>
> roundedNums = zeros(n, 1);
>
>
>
> signNums(normNums < 0) = -1;
>
> normNums(normNums < 0) = -normNums(normNums < 0);
>
>
>
>
>
> for i = 1:n
>
> while (normNums(i)-roundedNormNums(i)) > 0.5
>
> roundedNormNums(i) = roundedNormNums(i)+1;
>
> end
>
> end
>
>
>
> %% Calculates the Output
>
>
>
> roundedNums = signNums.*roundedNormNums.*digits;
>
> column_C = xlswrite('Round.xlsx', roundedNums , 'C', sprintf('C1:C%d', n));

Why not simply:
normNums = numbers./digits;
roundedNums=round(normNums).*digits;

Date Subject Author
5/4/14 Kris
5/4/14 Derek Goring