Skip to content

Commit d998196

Browse files
Fix range inputs (#554)
* Add decimal demo * Use CultureInfo.InvariantCulture for float, double & decimal type
1 parent bd5b0d1 commit d998196

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

BlazorBootstrap.Demo.RCL/Pages/Form/RangeInput/RangeInputDocumentation.razor

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
</div>
5353
<Demo Type="typeof(RangeInput_Demo_05_Tick_Marks)" Tabs="true" />
5454

55+
56+
<SectionHeading Size="HeadingSize.H2" Text="Decimals" PageUrl="@pageUrl" HashTagName="decimals" />
57+
<div class="mb-3"></div>
58+
<Demo Type="typeof(RangeInput_Demo_06_Decimals)" Tabs="false" />
5559
@code {
5660
private string pageUrl = "/form/range-input";
5761
private string title = "Blazor RangeInput Component";
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<RangeInput TValue="decimal" @bind-Value="amount1" Min="0" Max="100" Step="0.01" />
2+
@amount1
3+
@code {
4+
decimal amount1 = 0;
5+
}

blazorbootstrap/Components/Form/RangeInput/RangeInput.razor

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
type="range"
88
class="@($"{ClassNames} {fieldCssClasses} {(showTickMarks ? "bb-form-range-input" : "")}".Trim())"
99
id="@ElementId"
10-
min="@Min"
11-
max="@Max"
12-
step="@Step"
13-
value="@Value"
10+
min="@GetInvariantNumber(Min)"
11+
max="@GetInvariantNumber(Max)"
12+
step="@Step.ToString(CultureInfo.InvariantCulture)"
13+
value="@GetInvariantNumber(Value)"
1414
style="@StyleNames"
1515
list="@(showTickMarks ? $"datalist-{ElementId}" : "")"
1616
disabled="@Disabled"

blazorbootstrap/Components/Form/RangeInput/RangeInput.razor.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,31 @@ private void SetValue(object? newValue)
284284
Value = value;
285285
}
286286

287+
private string GetInvariantNumber(TValue value)
288+
{
289+
if (value is null)
290+
{
291+
return string.Empty;
292+
}
293+
294+
if (value is float floatValue)
295+
{
296+
return floatValue.ToString(CultureInfo.InvariantCulture);
297+
}
298+
299+
if (value is double doubleValue)
300+
{
301+
return doubleValue.ToString(CultureInfo.InvariantCulture);
302+
}
303+
304+
if (value is decimal decimalValue)
305+
{
306+
return decimalValue.ToString(CultureInfo.InvariantCulture);
307+
}
308+
309+
// All numbers without decimal places work fine by default
310+
return value?.ToString() ?? string.Empty;
311+
}
287312
private bool TryParseValue(object value, out TValue newValue)
288313
{
289314
try
@@ -323,23 +348,23 @@ private bool TryParseValue(object value, out TValue newValue)
323348
// float? / float
324349
if (typeof(TValue) == typeof(float?) || typeof(TValue) == typeof(float))
325350
{
326-
newValue = (TValue)Convert.ChangeType(value, typeof(float));
351+
newValue = (TValue)Convert.ChangeType(value, typeof(float), CultureInfo.InvariantCulture);
327352

328353
return true;
329354
}
330355

331356
// double? / double
332357
if (typeof(TValue) == typeof(double?) || typeof(TValue) == typeof(double))
333358
{
334-
newValue = (TValue)Convert.ChangeType(value, typeof(double));
359+
newValue = (TValue)Convert.ChangeType(value, typeof(double), CultureInfo.InvariantCulture);
335360

336361
return true;
337362
}
338363

339364
// decimal? / decimal
340365
if (typeof(TValue) == typeof(decimal?) || typeof(TValue) == typeof(decimal))
341366
{
342-
newValue = (TValue)Convert.ChangeType(value, typeof(decimal));
367+
newValue = (TValue)Convert.ChangeType(value, typeof(decimal), CultureInfo.InvariantCulture);
343368

344369
return true;
345370
}

0 commit comments

Comments
 (0)