CSharp · Console
using System;
using System.Globalization;
using System.Text;
//https://stackoverflow.com/questions/77982158/how-to-use-numberformatinfo
public class Program
{
public static void Main()
{
var nfi = new NumberFormatInfo();
// https://learn.microsoft.com/en-us/dotnet/api/system.globalization.numberformatinfo.numberdecimaldigits?view=net-8.0#remarks
// The NumberDecimalDigits property is used with the "F" and "N" standard format strings without a precision specifier in numeric formatting operations.
nfi.NumberDecimalDigits = 4; //<----- for point 1
//https://learn.microsoft.com/en-us/dotnet/api/system.globalization.numberformatinfo.numberdecimalseparator?view=net-8.0#remarks
//The NumberDecimalSeparator property is used with the "E", "F", "G", "N", and "R" standard format strings to define the symbol that separates integral from fractional digits.
nfi.NumberDecimalSeparator = "@"; //<----- for point 2
//https://learn.microsoft.com/en-us/dotnet/api/system.globalization.numberformatinfo.numbergroupseparator?view=net-8.0#remarks
//The NumberGroupSeparator property is used with the "N" standard format string to define the symbol that separates groups of integral digits. For more information, see Standard Numeric Format Strings.
nfi.NumberGroupSeparator = "^"; //<----- for point 3? or
//https://learn.microsoft.com/en-us/dotnet/api/system.globalization.numberformatinfo.permillesymbol?view=net-8.0#remarks
//The string assigned to the PerMilleSymbol property is included in the result string when a numeric value is formatted with a format string that includes the "‰" .
//https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings#the--custom-specifier-4
//A per mille character (‰ or \u2030) in a format string causes a number to be multiplied by 1000 before it is formatted. The appropriate per mille symbol is inserted in the returned string at the location where the ‰ symbol appears in the format string.
nfi.PerMilleSymbol = "#"; //<----- for point 3?
double aaa = -123456.789;
foreach (var formatString in new [] { "G", "N", "F", "P", "#0.## " + '\u2030'})
{
string str = aaa.ToString(formatString, nfi);
Console.WriteLine("aaa.ToString(\"{0}\") = {1}", formatString, str);
}
}
}