Formatting Floats with Decimal Precision
In this instance, you're attempting to display a floating-point value while maintaining a precise decimal format. Here's how you can achieve this:
To format the floating-point value Sale, you can utilize the ToString method with a specified format string. This allows you to control the display format of your output, including the number of decimal places.
For your case, you can use the following format specifiers:
Here's a modified version of your code that formats the Sale value with 2 decimal places:
Sale = float.Parse(((x.Sale_Price - (x.Sale_Price * (x.Discount_Price / 100))).ToString())).ToString("n2");
Using the "n2" format, your output will be displayed with 2 decimal places and commas for easier readability.
The above is the detailed content of How Can I Format a Floating-Point Number with Two Decimal Places in C#?. For more information, please follow other related articles on the PHP Chinese website!