@manhng

Welcome to my blog!

Number Formatting

June 23, 2021 10:20

Number Formatting (edit)

NumberFormatInfo.CurrentInfo Property (System.Globalization) | Microsoft Docs

Standard numeric format strings | Microsoft Docs

Number Formatting - Globalization | Microsoft Docs

Namespace: System.Globalization
Assembly: System.Runtime.dll

enter image description here

Format a number with commas and decimals in C# (asp.net MVC3) - Stack Overflow

C# - How to change symbol for decimal point in double.ToString()? - Stack Overflow

How to round up or down in C#?

c# math round down Code Example (codegrepper.com)

decimal.Round(x, 2)
decimal.Round(x, 2, MidpointRounding.AwayFromZero);

Program.cs

using System;
using System.Globalization;

namespace ConsoleApp2
{
internal class Program
{
private static void Main(string[] args)
{
//var x = Math.Floor(-1.23 * 10) / 10; //==> -1.3
var x = Math.Floor(0m * 10) / 10; //==> 0
//var x = Math.Floor(1.23 * 10) / 10; //==> 1.2
//var x = Math.Floor(0.99 * 10) / 10; //==> 0.9
//var x = Math.Floor(1.87 * 10) / 10; //==> 1.8
Console.WriteLine(x);

//Console.WriteLine( CultureInfo.CurrentUICulture.Name);

NumberFormatInfo nfi = NumberFormatInfo.CurrentInfo;
Console.WriteLine(nfi.NumberDecimalSeparator);
Console.WriteLine(nfi.NumberGroupSeparator);

//NumberFormatInfo nfi = new NumberFormatInfo();
//nfi.NumberDecimalSeparator = ",";
//nfi.NumberGroupSeparator = ".";

double num = 4.36;
string ret = num.ToString(nfi); // 4,3
Console.WriteLine(ret);

//string s0 = string.Format(System.Globalization.CultureInfo.GetCultureInfo("en-US"), "{0:0.0}", 4.35);
//Console.WriteLine(s0);
//string s1 = string.Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE"), "{0:0.0}", 4.35);
//Console.WriteLine(s1);
//string s2 = string.Format(System.Globalization.CultureInfo.GetCultureInfo("ja-JP"), "{0:0.0}", 4.35);
//Console.WriteLine(s2);
//string s3 = string.Format(CultureInfo.CurrentUICulture, "{0:0.0}", 14524.35);
//Console.WriteLine(s3);
}

/// <summary>
///
/// </summary>
/// <param name="tax"></param>
/// <returns></returns>
private static decimal RoundTax(decimal tax)
{
if (tax <= 0) return 0;
return Math.Floor(0m * 10) / 10;
}
}
}

Làm tròn xuống

return Math.Floor(1m * 10) / 10;

Categories

Recent posts