Share 2 decimal places tostring c#

trantito

New member
** Cách làm tròn đến 2 vị trí thập phân trong C#**

Các số làm tròn đến một số lượng số thập phân cụ thể là một nhiệm vụ phổ biến trong lập trình.Trong C#, bạn có thể sử dụng phương thức `vòng ()` để làm tròn một số vào bất kỳ số vị trí thập phân nào.Phương thức `vòng ()` có hai đối số: số lượng được làm tròn và số lượng vị trí thập phân để làm tròn.

Ví dụ: mã sau làm tròn số 1.234567 đến 2 số thập phân:

`` `C#
số gấp đôi = 1.234567;
Double RoundNumber = math.Round (số, 2);

// RoundedNumber hiện bằng 1,23
`` `

Bạn cũng có thể sử dụng phương thức `vòng ()` để làm tròn một số lên hoặc xuống.Để làm tròn, chuyển một số âm làm đối số thứ hai cho phương thức `vòng ()`.Để làm tròn xuống, chuyển một số dương làm đối số thứ hai cho phương thức `vòng ()`.

Ví dụ: mã sau làm tròn số 1.234567 lên đến 2 vị trí thập phân:

`` `C#
số gấp đôi = 1.234567;
Double RoundNumber = math.Round (số, 2, midpointrounding.awayfromzero);

// RoundedNumber hiện bằng 1,24
`` `

Mã sau làm tròn số 1.234567 xuống 2 số thập phân:

`` `C#
số gấp đôi = 1.234567;
Double RoundNumber = math.Round (số, 2, midpointrounding.towardzero);

// RoundedNumber hiện bằng 1,23
`` `

## Bài viết tham khảo

* [Cách làm tròn một số trong C#] (https://www.tutorialspoint.com/csharp/csharp_rounding_numbers.htm)
* [Số làm tròn trong C#] (Math.Round Method (System))

## hashtags

* #csharp
* #Programming
* #phát triển
* #Rounding
* #decimal_places
=======================================
**How to Round to 2 Decimal Places in C#**

Rounding numbers to a specific number of decimal places is a common task in programming. In C#, you can use the `Round()` method to round a number to any number of decimal places. The `Round()` method takes two arguments: the number to be rounded and the number of decimal places to round to.

For example, the following code rounds the number 1.234567 to 2 decimal places:

```c#
double number = 1.234567;
double roundedNumber = Math.Round(number, 2);

// roundedNumber is now equal to 1.23
```

You can also use the `Round()` method to round a number up or down. To round up, pass a negative number as the second argument to the `Round()` method. To round down, pass a positive number as the second argument to the `Round()` method.

For example, the following code rounds the number 1.234567 up to 2 decimal places:

```c#
double number = 1.234567;
double roundedNumber = Math.Round(number, 2, MidpointRounding.AwayFromZero);

// roundedNumber is now equal to 1.24
```

The following code rounds the number 1.234567 down to 2 decimal places:

```c#
double number = 1.234567;
double roundedNumber = Math.Round(number, 2, MidpointRounding.TowardZero);

// roundedNumber is now equal to 1.23
```

## Reference Articles

* [How to Round a Number in C#](https://www.tutorialspoint.com/csharp/csharp_rounding_numbers.htm)
* [Rounding Numbers in C#](https://docs.microsoft.com/en-us/dotnet/api/system.math.round?view=net-6.0)

## Hashtags

* #csharp
* #Programming
* #development
* #Rounding
* #decimal_places
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top