-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathCurrencyNumberFormatter.cs
67 lines (52 loc) · 2.09 KB
/
CurrencyNumberFormatter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System.Collections.Generic;
using System.Linq;
using Windows.Globalization.NumberFormatting;
namespace Telegram.Common
{
public partial class CurrencyNumberFormatter : INumberFormatter2, INumberParser
{
private readonly CurrencyFormatter _formatter;
private readonly string _currencySymbol;
public CurrencyNumberFormatter(string currencyCode, IEnumerable<string> languages, string geographicRegion)
{
if (string.IsNullOrEmpty(currencyCode))
{
currencyCode = "USD";
}
var formatter = new CurrencyFormatter(currencyCode, languages, geographicRegion);
var formatted = formatter.Format(0);
var splitted = formatted.Split('\u00A0');
for (int i = 0; i < splitted.Length; i++)
{
if (splitted[i].Any(x => char.IsDigit(x)))
{
continue;
}
_currencySymbol = splitted[i];
}
_formatter = formatter;
}
public string Format(double value) => _formatter.Format(value);
public string FormatInt(long value) => _formatter.FormatInt(value);
public string FormatUInt(ulong value) => _formatter.FormatUInt(value);
public string FormatDouble(double value) => _formatter.FormatDouble(value);
public long? ParseInt(string text) => _formatter.ParseInt(ValidateInput(text));
public ulong? ParseUInt(string text) => _formatter.ParseUInt(ValidateInput(text));
public double? ParseDouble(string text) => _formatter.ParseDouble(ValidateInput(text));
private string ValidateInput(string text)
{
var trim = text.Trim();
if (trim.Length > 0 && char.IsDigit(trim[trim.Length - 1]))
{
return $"{trim} {_currencySymbol}";
}
return text;
}
}
}