J'ai un numéro comme 202667.4
. Je veux le convertir en nombre basé sur la culture .
Pour Ex:
En "de" (allemand), le numéro doit être en 202.667,40 .
Toute aide sera fortement appréciée.
Merci.
4 réponses
Si vous souhaitez représenter un nombre existant (par exemple, double
) dans un format spécifique à la culture, essayez le formatage :
https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
double source = 202667.4;
// "n" - ... group separators, and a decimal separator with optional negative sign
// "de" - German culture
string result = source.ToString("n", CultureInfo.GetCultureInfo("de"));
Console.WriteLine(result);
Résultat
202.667,40
Si vous recevez un string
et que vous voulez un nombre, mettez Parse
(TryParse
):
string data = "202.667,40";
double result = double.Parse(data, CultureInfo.GetCultureInfo("de"));
Console.WriteLine(data.ToString(CultureInfo.InvariantCulture));
Si vous ne souhaitez pas spécifier la culture à chaque fois que vous travaillez avec la mise en forme, vous pouvez définir la culture comme actuelle :
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("de");
...
double source = 202667.4;
Console.WriteLine($"{source:n}");
Vous pouvez utiliser les informations de culture tout en analysant le nombre au format allemand
Essayez avec cette approche:
string.Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE"), "{0:000,000.00}", <your number>)
Par exemple:
string result = string.Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE"), "{0:000,000.00}", 202667.4)
Vous pouvez utiliser ce code pour convertir des variables en différentes cultures:
int MyInt = 100;
string MyString = MyInt.ToString("C",CultureInfo.GetCultureInfo("de-DE"));
MessageBox.Show(MyString);
Si vous souhaitez créer tous les programmes au format allemand, vous pouvez ce code dans votre classe principale:
using System.Globalization;`
Application.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");`
System.Globalization.CultureInfo EnglishCulture = new
System.Globalization.CultureInfo("en-EN");
System.Globalization.CultureInfo GermanCulture = new
System.Globalization.CultureInfo("de-de");
Transformation nécessaire,
double val;
if(double.TryParse("65,89875", System.Globalization.NumberStyles.Float,
GermanCulture, out val))
{
string valInGermanFormat = val.ToString(GermanCulture);
string valInEnglishFormat = val.ToString(EnglishCulture);
}
if(double.TryParse("65.89875", System.Globalization.NumberStyles.Float,
EnglishCulture, out val))
{
string valInGermanFormat = val.ToString(GermanCulture);
string valInEnglishFormat = val.ToString(EnglishCulture);
}
Questions connexes
De nouvelles questions
c#
C # (prononcé "see sharp") est un langage de programmation multi-paradigme de haut niveau, typé statiquement développé par Microsoft. Le code C # cible généralement la famille d'outils et d'exécutions Microsoft .NET, notamment le .NET Framework, .NET Core et Xamarin. Utilisez cette balise pour les questions sur le code écrit en C # ou en spécification formelle de C #.