在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
在 C# 中將 double 四捨五入為整數 是編程中經常出現的基本任務,尤其是在計算產生 double 值但需要整數值進行進一步操作時更為普遍。 該過程涉及將可能包含小數位的雙精度值轉換為最接近的整數。 這可以使用多種方法完成,每種方法都遵循指定的四捨五入規則。
在本指南中,我們將探索在C#中用於將雙精度值四捨五入為整數位的方法和函數,幫助開發人員了解每種方法的影響和應用。 我們還將探索IronPDF .NET PDF Library的功能,這是一個用於在C#中創建PDF文檔的強大工具。
在 C# 中,Math.Round 方法是將 double 值四捨五入至最接近整數的主要工具。 此函數將 double 值四捨五入至最接近的整數值,並通過允許指定特定數量的小數位及四捨五入策略的重載提供對四捨五入過程的高度控制。 例如,當一個雙精度值恰好介於兩個整數之間時,四捨五入的規則會決定該值是向上取整還是向下取整。
以下是一個Math.Round方法的基本範例:
public static void Main()
{
double myDouble = 9.5;
int myInt = (int)Math.Round(myDouble);
Console.WriteLine("The rounded integer value is: " + myInt);
}
public static void Main()
{
double myDouble = 9.5;
int myInt = (int)Math.Round(myDouble);
Console.WriteLine("The rounded integer value is: " + myInt);
}
Imports System
Public Shared Sub Main()
Dim myDouble As Double = 9.5
Dim myInt As Integer = CInt(Math.Truncate(Math.Round(myDouble)))
Console.WriteLine("The rounded integer value is: " & myInt)
End Sub
在此原始碼中,Math.Round 用於將雙精度值 9.5 四捨五入到最接近的整數。 該方法返回10,因為它四捨五入到最接近的數字。 這是當四捨五入正值且該值恰好位於兩個整數值之間時的預期結果。
在 C# 中將 double 值轉換為整數值的另一種常見方法是透過顯式轉換。 顯式轉換涉及直接將 double 類型轉換為 int 類型,這會截去任何小數位。 這意味著它不是四捨五入到最接近的整數,而是向下取整到最接近的小的整數。 當您只需要去除小數位而不考慮最接近的整數值時,此方法非常有用。
以下是執行顯式轉換的方法:
public static void Main()
{
double myDouble = 9.9;
int myInt = (int)myDouble;
Console.WriteLine("The integer value after explicit conversion is: " + myInt);
}
public static void Main()
{
double myDouble = 9.9;
int myInt = (int)myDouble;
Console.WriteLine("The integer value after explicit conversion is: " + myInt);
}
Imports System
Public Shared Sub Main()
Dim myDouble As Double = 9.9
Dim myInt As Integer = CInt(Math.Truncate(myDouble))
Console.WriteLine("The integer value after explicit conversion is: " & myInt)
End Sub
在上面的例子中,輸出將是9,因為顯式轉換只是去掉了9.9的小數位,導致得出較小的整數。 此方法快捷,但在需要根據特定捨入規則進行精確捨入時可能不適用。
除了Math.Round和顯式轉換之外,C#還提供其他用於圓整雙精度值的方法,以滿足不同的需求。 例如,Math.Floor 和 Math.Ceiling 分別提供將雙精度值始終向較小或較大整數舍入的選項。 Math.Floor 對於即使是負數值也總是向下取整特別有用,而 Math.Ceiling 則保證向上取整。
讓我們來看看這些方法的例子:
public static void Main()
{
double myDouble = 9.2;
int floorInt = (int)Math.Floor(myDouble);
int ceilingInt = (int)Math.Ceiling(myDouble);
Console.WriteLine("Rounded down: " + floorInt);
Console.WriteLine("Rounded up: " + ceilingInt);
}
public static void Main()
{
double myDouble = 9.2;
int floorInt = (int)Math.Floor(myDouble);
int ceilingInt = (int)Math.Ceiling(myDouble);
Console.WriteLine("Rounded down: " + floorInt);
Console.WriteLine("Rounded up: " + ceilingInt);
}
Imports System
Public Shared Sub Main()
Dim myDouble As Double = 9.2
Dim floorInt As Integer = CInt(Math.Truncate(Math.Floor(myDouble)))
Dim ceilingInt As Integer = CInt(Math.Truncate(Math.Ceiling(myDouble)))
Console.WriteLine("Rounded down: " & floorInt)
Console.WriteLine("Rounded up: " & ceilingInt)
End Sub
在上述程式碼中,Math.Floor 將 9.2 捨入至較少小數位數的最近整數,返回 9,而 Math.Ceiling 則向下一個正整數進位,返回 10。 當四捨五入策略必須無歧義地傾向於較高或較低的整數值時,這些方法是必不可少的。
探索 IronPDF 功能,以了解這個 .NET 程式庫如何使 C# 開發人員直接從 HTML 创建和管理 PDF 文件。 它使用 Chrome 渲染引擎來確保 PDF 看起來就像在網頁瀏覽器中一樣。 這使其非常適合創建基於網頁的報告。 IronPDF 能夠處理複雜的任務,例如添加數位簽名、更改文檔佈局,以及插入自定義的頁首、頁尾或浮水印。 使用起來很簡單,因為它允許開發人員使用熟悉的網頁技術,如 HTML、CSS、JavaScript 和圖像來製作或編輯 PDF 文件。
使用 IronPDF,主要功能是將HTML 轉換為 PDF 使用 IronPDF,同時保持佈局和樣式。 它可以從各種網頁內容生成 PDF,例如報告、發票和文件,將 HTML 文件、URL 或 HTML 字串轉換為 PDF 文件。
using IronPdf;
class Program
{
static void Main(string[] args)
{
var renderer = new ChromePdfRenderer();
// 1. Convert HTML String to PDF
var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");
// 2. Convert HTML File to PDF
var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");
// 3. Convert URL to PDF
var url = "http://ironpdf.com"; // Specify the URL
var pdfFromUrl = renderer.RenderUrlAsPdf(url);
pdfFromUrl.SaveAs("URLToPDF.pdf");
}
}
using IronPdf;
class Program
{
static void Main(string[] args)
{
var renderer = new ChromePdfRenderer();
// 1. Convert HTML String to PDF
var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");
// 2. Convert HTML File to PDF
var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");
// 3. Convert URL to PDF
var url = "http://ironpdf.com"; // Specify the URL
var pdfFromUrl = renderer.RenderUrlAsPdf(url);
pdfFromUrl.SaveAs("URLToPDF.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim renderer = New ChromePdfRenderer()
' 1. Convert HTML String to PDF
Dim htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>"
Dim pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent)
pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf")
' 2. Convert HTML File to PDF
Dim htmlFilePath = "path_to_your_html_file.html" ' Specify the path to your HTML file
Dim pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath)
pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf")
' 3. Convert URL to PDF
Dim url = "http://ironpdf.com" ' Specify the URL
Dim pdfFromUrl = renderer.RenderUrlAsPdf(url)
pdfFromUrl.SaveAs("URLToPDF.pdf")
End Sub
End Class
為了將 IronPDF 與 C# 的四捨五入功能結合,開發人員可以將 IronPDF 的 PDF 生成功能與 C# 中的數學運算結合起來。 這在財務或報告應用中特別有用,因為需要清晰和準確地呈現數據。 例如,您可以生成發票或財務摘要,將數字四捨五入到最接近的整數,以確保可讀性並符合會計標準。
以下是一個示例,說明如何使用IronPDF和C#的Math.Round方法來創建包含四捨五入數據的PDF:
using IronPdf;
using System;
public class PDFGenerationWithRounding
{
public static void Main()
{
License.LicenseKey = "License-Key";
// Initialize the HTML to PDF renderer
var renderer = new ChromePdfRenderer();
// Example data
double transactionAmount = 123.456;
int roundedAmount = (int)Math.Round(transactionAmount);
// HTML content including the rounded amount
string htmlContent = $@"
<html>
<head>
<title>Transaction Summary</title>
</head>
<body>
<h1>Transaction Details</h1>
<p>Original Amount: ${transactionAmount}</p>
<p>Rounded Amount: ${roundedAmount}</p>
</body>
</html>";
// Convert the HTML to a PDF document
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("TransactionSummary.pdf");
Console.WriteLine("The PDF document has been generated with rounded figures.");
}
}
using IronPdf;
using System;
public class PDFGenerationWithRounding
{
public static void Main()
{
License.LicenseKey = "License-Key";
// Initialize the HTML to PDF renderer
var renderer = new ChromePdfRenderer();
// Example data
double transactionAmount = 123.456;
int roundedAmount = (int)Math.Round(transactionAmount);
// HTML content including the rounded amount
string htmlContent = $@"
<html>
<head>
<title>Transaction Summary</title>
</head>
<body>
<h1>Transaction Details</h1>
<p>Original Amount: ${transactionAmount}</p>
<p>Rounded Amount: ${roundedAmount}</p>
</body>
</html>";
// Convert the HTML to a PDF document
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("TransactionSummary.pdf");
Console.WriteLine("The PDF document has been generated with rounded figures.");
}
}
Imports IronPdf
Imports System
Public Class PDFGenerationWithRounding
Public Shared Sub Main()
License.LicenseKey = "License-Key"
' Initialize the HTML to PDF renderer
Dim renderer = New ChromePdfRenderer()
' Example data
Dim transactionAmount As Double = 123.456
Dim roundedAmount As Integer = CInt(Math.Truncate(Math.Round(transactionAmount)))
' HTML content including the rounded amount
Dim htmlContent As String = $"
<html>
<head>
<title>Transaction Summary</title>
</head>
<body>
<h1>Transaction Details</h1>
<p>Original Amount: ${transactionAmount}</p>
<p>Rounded Amount: ${roundedAmount}</p>
</body>
</html>"
' Convert the HTML to a PDF document
Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)
pdf.SaveAs("TransactionSummary.pdf")
Console.WriteLine("The PDF document has been generated with rounded figures.")
End Sub
End Class
在此範例中,IronPDF 將一個簡單的 HTML 字串轉換為 PDF 文件,並合併包含四捨五入至最接近整數的交易金額的動態數據。 這種方法高度適應性強,允許開發人員創建更複雜的文件,以滿足其具體需求,並在整個過程中突出精確性和易用性。
在 C# 中將 double 轉換為 int 是一個多變的過程,受到 double 值的性質、四捨五入的上下文以及應用程式所需精度的影響。 無論是使用Math.Round進行最接近整數的四捨五入,透過明確轉換進行直接截斷,或使用其他方法如Math.Floor和Math.Ceiling來指定四捨五入方向,C# 提供的方法都可以有效處理雙精度值的四捨五入。 IronPDF 提供IronPDF 的免費試用,價格從$749起。