# Matlab rounding reserved to specified decimals

Source: Internet
Author: User

`Reprint Source: HTTP://BLOG.CSDN.NET/YF210YF`

Matlab takes the whole function: fix, floor, ceil, round. The rounding function is useful in programming. First, take the whole function 1. To 0 rounding (truncated rounding)

fix-to 0 rounding (Round towards zero);

>> Fix (3.6)

Ans = 3 2. Negative Infinity rounding (maximum integer-Gaussian rounding with no more than X)

floor-to negative infinite rounding (Round towards minus infinity);

>> Floor (-3.6)

Ans =-4 3. Rounding to positive infinity (smallest integer greater than x)

ceil-to positive infinity (Round towards plus infinity);

>> Ceil (-3.6)

Ans =-3 4. Rounding to the nearest integer, rounded (rounded rounding)

Round-rounding (round towards nearest integer) to the nearest integer;

>> Round (3.5)

Ans = 4 Second, after the decimal point a rounding, that is, to retain a few decimals, also often used. 1. Numerical type roundn-arbitrary bit position rounding

>>a=123.4567890;

>>A=ROUNDN (a,-4)

A = 123.4568

where the ROUNDN function functions as follows:

y = Roundn (x) rounds the input data x to the nearest hundredth. % does not specify N, accurate to percentile y = Roundn (x,n) rounds the input data x at the specified power% to specify the number of digits after the decimal point n

2. Symbol Type

Digits (4)

VPA (...)

It is important to note that the VPA command does not recognize integers and decimals, only the total number of digits, so it is necessary for a decimal integer to be a single digit, for example, to retain two decimal places for 9.3154:

>>a=9.3154;

>>digits (3)

>>B=VPA (a)

b= 9.32

where b is a symbolic variable; 3. Character Type

>>a=12.34567;

>>b = sprintf ('%8.2f ', a)

b = 12.35 where B is a character variable.

matlab Text Output

Two functions: disp

fprintf 1, function disp with only one variable, he can be a conceited matrix or a numerical matrix, to output a simple text message, only need to enclose the information in single quotation marks:

>>disp (' My favorite color is red ');

Or

>>yourname=input (' Enter your name ', ' s ');

>>disp ([' Your name is ', Youname]);

For example

>> yourname = input (' Enter your name ', ' s ');

>> disp ([' Your name is ', yourname]);

When selecting text information with numeric variable values, you need to convert the type of the numeric variable to the character type using the function num2str

>> x=98;

>> outstring = [' x = ', Num2str (x)];

>> disp (outstring);

x = 98

>> disp ([' x = ', Num2str (x)]);

x = 98

The DISP function can take only one variable, and the columns in the table need to be combined into a matrix, as shown in the following program.

>> x=0:pi/5:pi;y=sin (x);

>> disp ([x ' Y ']);

0 0

0.6283 0.5878

1.2566 0.9511

1.8850 0.9511

2.5133 0.5878

3.1416 0.0000

Format command

Controls the display mode until the next format appears, and the format command remains in effect.

>> x=1.23456789;

>> format short;disp (PI);

3.1416

>> format long;disp (PI);

3.141592653589793

>> format Short e;disp (PI);

3.1416e+000

>> format +;d ISP (PI);

+

>> format bank;disp (PI);

3.14

2. Function fprintf

fprintf (format);

fprintf (Format,variables);

fprintf (Fid,format,variables);

For example:

>> fprintf (' I am concreten ');

I am concrete

>> a=3;b= ' s ';

>> fprintf (' This is a%d and%s n ', A, b);

This is a 3 and s

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.