Précision maximale pour les chaînes de format numérique
La précision maximale lors de la mise en forme des nombres sous forme de chaînes à l’aide de ToString
et TryFormat
a été modifiée de Int32.MaxValue à 999 999 999. (La précision maximale a été précédemment changée en Int32.MaxValue dans .NET 6.)
En outre, l’exposant maximal autorisé lors de l’analyse d’un BigInteger à partir d’une chaîne a été limité à 999 999 999.
Comportement précédent
Dans .NET 6, la logique d’analyse de format numérique standard était limitée à une précision de Int32.MaxValue ou moins. Le comportement prévu était de lever FormatException pour toute précision supérieure à Int32.MaxValue. Toutefois, en raison d’un bogue, .NET 6 ne levait pas cette exception pour certaines entrées de ce type. Le comportement prévu était le suivant :
double d = 123.0;
d.ToString("E" + int.MaxValue.ToString()); // Doesn't throw.
long intMaxPlus1 = (long)int.MaxValue + 1;
string intMaxPlus1String = intMaxPlus1.ToString();
Assert.Throws<FormatException>(() => d.ToString("E" + intMaxPlus1String)); // Throws.
En outre, la taille de l’exposant n’était pas limitée lors de l’analyse d’un BigInteger à partir d’une chaîne.
Nouveau comportement
À compter de .NET 7, .NET prend en charge la précision jusqu’à 999 999 999. Une exception FormatException est levée si la précision est supérieure à 999 999 999. Cette modification a été implémentée dans la logique d’analyse qui affecte tous les types numériques.
double d = 123.0;
Assert.Throws<FormatException>(() => d.ToString("E" + int.MaxValue.ToString())); // Throws.
long intMaxPlus1 = (long)int.MaxValue + 1;
string intMaxPlus1String = intMaxPlus1.ToString();
Assert.Throws<FormatException>(() => d.ToString("E" + intMaxPlus1String)); // Throws.
d.ToString("E999999999"); // Doesn't throw.
d.ToString("E00000999999999"); // Doesn't throw.
En outre, si vous essayez d’analyser un BigInteger avec un exposant supérieur à 999 999 999 à partir d’une chaîne, une exception FormatException est levée.
Version introduite
.NET 7
Type de changement cassant
Ce changement peut affecter la compatibilité binaire.
Raison de la modification
Le comportement introduit dans .NET 6 était destiné à lever FormatException pour toute précision supérieure à Int32.MaxValue. Toutefois, en raison d’un bogue, cette exception n’était pas levée pour certaines entrées supérieures à la valeur maximale. Cette modification corrige le bogue en limitant la précision à 999 999 999.
Action recommandée
Dans la plupart des cas, aucune action n’est nécessaire, car il est peu probable que vous utilisiez déjà une précision supérieure à 999 999 999 dans vos chaînes de format.
API affectées
Cette modification a été implémentée dans la logique d’analyse qui affecte tous les types numériques.
- System.Numerics.BigInteger.ToString(String)
- System.Numerics.BigInteger.ToString(String, IFormatProvider)
- System.Numerics.BigInteger.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)
- System.Int32.ToString(String)
- System.Int32.ToString(String, IFormatProvider)
- System.Int32.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)
- System.UInt32.ToString(String)
- System.UInt32.ToString(String, IFormatProvider)
- System.UInt32.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)
- System.Byte.ToString(String)
- System.Byte.ToString(String, IFormatProvider)
- System.Byte.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)
- System.SByte.ToString(String)
- System.SByte.ToString(String, IFormatProvider)
- System.SByte.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)
- System.Int16.ToString(String)
- System.Int16.ToString(String, IFormatProvider)
- System.Int16.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)
- System.UInt16.ToString(String)
- System.UInt16.ToString(String, IFormatProvider)
- System.UInt16.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)
- System.Numerics.BigInteger.Parse
- System.Numerics.BigInteger.TryParse
- System.Int64.ToString(String)
- System.Int64.ToString(String, IFormatProvider)
- System.Int64.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)
- System.UInt64.ToString(String)
- System.UInt64.ToString(String, IFormatProvider)
- System.UInt64.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)
- System.Half.ToString(String)
- System.Half.ToString(String, IFormatProvider)
- System.Half.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)
- System.Single.ToString(String)
- System.Single.ToString(String, IFormatProvider)
- System.Single.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)
- System.Double.ToString(String)
- System.Double.ToString(String, IFormatProvider)
- System.Double.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)
- System.Decimal.ToString(String)
- System.Decimal.ToString(String, IFormatProvider)
- System.Decimal.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)
Voir aussi
Commentaires
https://aka.ms/ContentUserFeedback.
Bientôt disponible : Tout au long de 2024, nous allons supprimer progressivement GitHub Issues comme mécanisme de commentaires pour le contenu et le remplacer par un nouveau système de commentaires. Pour plus d’informations, consultezEnvoyer et afficher des commentaires pour