mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 20:27:36 +02: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:
@@ -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