Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

i want a programme to approximate decimal to number

Asked by karim

karim (view profile)

on 29 Apr 2013

i want a programme to approximate decimal to number

EX:

if I have 2.1 or 2.3 or 2.3 or 2.4

I want the answer to be 2

and if i have 2.5 till to 2.9

I want the answer to be 3

but the number is variable

I mean (2) can be 1,2,3,4,.....and so on

any number i can enter it

0 Comments

karim

karim (view profile)

Products

No products are associated with this question.

1 Answer

Answer by the cyclist

the cyclist (view profile)

on 29 Apr 2013
Edited by the cyclist

the cyclist (view profile)

on 29 Apr 2013
Accepted answer

Do you just need the round() function?

>> round(2.3)
ans = 
     2

10 Comments

Walter Roberson

Walter Roberson (view profile)

on 29 Apr 2013
myround( input('Enter number to be rounded') )
karim

karim (view profile)

on 29 Apr 2013

Thanks i solve it

yourVariable=input('yourVariable=');
roundedNumber = round(yourVariable)

But i want to know also

how this code working

function r = myround(x)
  r = round(x);
end

and then

myround(2.3)
Jan Simon

Jan Simon (view profile)

on 29 Apr 2013

@Karim: Here the code is saved to a "function". The Getting Started chapters of the documentation explain the using of functions exhaustively, such that it is recommended to read them. It is not the job of the forum to repeat the basics, when all required information are found in the docs already.

the cyclist

the cyclist (view profile)

Contact us