How do change decimal place in command window?

1 visualización (últimos 30 días)
Hexus
Hexus el 29 de Abr. de 2012
I found a code that rounds any element of an array to any decimal place without using built in functions. It works good, but all the results in the command window are expressed in 10^-3. What needs to be done to express the results in their initial value.
Here's the code, so you can run it and see what I'm talking about.
clc;clear;format compact
numbers=[934.9 -0.4567 1045.76]; %get the number array
digits=[100 0.1 10]; %get the digit array
result=0*numbers;
% result=RoundTo(numbers, digits); % use function RoundTo to round array of numbers
for count0=1:length(numbers)
number=numbers(count0);
digit=digits(count0);
num_sign=1; % save the sign of number
if number<0
num_sign=-1;
end
GNumber=num_sign*number;
GFactor=1/digit;% change all digit to 1 by multiply by GFactor
GNumber=GNumber*GFactor;
int_num=0; %to save the integer part of GNumber
%find the integer part of GNumber
if GNumber>=1
max_order=0;
count=0;
decimal=1;
factor=zeros(1,500);
while decimal<GNumber % compare GNumber with decimal = 1, 10, 100, 1000......
count=count+1;
decimal=10*decimal;
end
max_order=count;
count_order=max_order;
Temp_GNumber=GNumber;
CNumber=0;
while decimal>=1 %determine factor is coresponded with each decimal
decimal=decimal/10;
count=0;
CNumber=count*decimal;
while CNumber<Temp_GNumber
count=count+1;
CNumber=count*decimal;
end
if decimal>=1
Temp_GNumber=Temp_GNumber-(count-1)*decimal;
factor(count_order)=count-1;
count_order=count_order-1;
end
end
decimal=1;
for count=1:max_order %caculate integer part base on factors are detemined
int_num=int_num+decimal*factor(count);
decimal=decimal*10;
end
end
if (GNumber-int_num)>=0.5
int_num=int_num+1;
end
output=num_sign*int_num/GFactor;
result(count0)=output;
end
output0=[numbers,digits,result] % display the result on the command screen of matlab
  1 comentario
Walter Roberson
Walter Roberson el 29 de Abr. de 2012
That code uses quite a number of built-in functions. For example, number < 0 uses the built-in function named "lt", and [numbers,digits,result] uses the built-in function named "horzcat".

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 29 de Abr. de 2012
format long g

Categorías

Más información sobre Operators and Elementary Operations en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by