mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 03:37:59 +01:00
[PTRun][Calc]Keep leading zeroes on languages where . is not a separator (#28451)
* [PTRun][Calc]Keep leading zeroes on languages where . is not a separator * Adapt tests
This commit is contained in:
@@ -132,12 +132,28 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow("12,0004", "12.0004")]
|
||||
[DataRow("0xF000", "0xF000")]
|
||||
public void Translate_NoRemovalOfLeadingZeroesOnEdgeCases(string input, string expectedResult)
|
||||
[DataRow("de-DE", "12,0004", "12.0004")]
|
||||
[DataRow("de-DE", "0xF000", "0xF000")]
|
||||
[DataRow("de-DE", "0", "0")]
|
||||
[DataRow("de-DE", "00", "0")]
|
||||
[DataRow("de-DE", "12.004", "12004")] // . is the group separator in de-DE
|
||||
[DataRow("de-DE", "12.04", "1204")]
|
||||
[DataRow("de-DE", "12.4", "124")]
|
||||
[DataRow("de-DE", "3.004.044.444,05", "3004044444.05")]
|
||||
[DataRow("de-DE", "123.01 + 52.30", "12301 + 5230")]
|
||||
[DataRow("de-DE", "123.001 + 52.30", "123001 + 5230")]
|
||||
[DataRow("fr-FR", "0", "0")]
|
||||
[DataRow("fr-FR", "00", "0")]
|
||||
[DataRow("fr-FR", "12.004", "12.004")] // . is not decimal or group separator in fr-FR
|
||||
[DataRow("fr-FR", "12.04", "12.04")]
|
||||
[DataRow("fr-FR", "12.4", "12.4")]
|
||||
[DataRow("fr-FR", "12.0004", "12.0004")]
|
||||
[DataRow("fr-FR", "123.01 + 52.30", "123.01 + 52.30")]
|
||||
[DataRow("fr-FR", "123.001 + 52.30", "123.001 + 52.30")]
|
||||
public void Translate_NoRemovalOfLeadingZeroesOnEdgeCases(string sourceCultureName, string input, string expectedResult)
|
||||
{
|
||||
// Arrange
|
||||
var translator = NumberTranslator.Create(new CultureInfo("de-de", false), new CultureInfo("en-US", false));
|
||||
var translator = NumberTranslator.Create(new CultureInfo(sourceCultureName, false), new CultureInfo("en-US", false));
|
||||
|
||||
// Act
|
||||
var result = translator.Translate(input);
|
||||
|
||||
@@ -36,8 +36,8 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
}
|
||||
|
||||
// check for division by zero
|
||||
// We check if the string contains a slash followed by space (optional) and zero. Whereas the zero must not followed by dot or comma as this indicates a number with decimal digits.
|
||||
if (new Regex("\\/\\s*0(?![,\\.])").Match(input).Success)
|
||||
// We check if the string contains a slash followed by space (optional) and zero. Whereas the zero must not followed by dot or comma as this indicates a number with decimal digits. The zero must also not be followed by other digits.
|
||||
if (new Regex("\\/\\s*0(?![,\\.0-9])").Match(input).Success)
|
||||
{
|
||||
error = Properties.Resources.wox_plugin_calculator_division_by_zero;
|
||||
return default;
|
||||
|
||||
@@ -76,10 +76,30 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
string[] tokens = splitRegex.Split(input);
|
||||
foreach (string token in tokens)
|
||||
{
|
||||
int leadingZeroCount = 0;
|
||||
|
||||
// Count leading zero characters.
|
||||
foreach (char c in token)
|
||||
{
|
||||
if (c != '0')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
leadingZeroCount++;
|
||||
}
|
||||
|
||||
// number is all zero characters. no need to add zero characters at the end.
|
||||
if (token.Length == leadingZeroCount)
|
||||
{
|
||||
leadingZeroCount = 0;
|
||||
}
|
||||
|
||||
decimal number;
|
||||
|
||||
outputBuilder.Append(
|
||||
decimal.TryParse(token, NumberStyles.Number, cultureFrom, out number)
|
||||
? number.ToString(cultureTo)
|
||||
? (new string('0', leadingZeroCount) + number.ToString(cultureTo))
|
||||
: token);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user