The latest release can be downloaded via NuGet.
dotnet-price-parser
is an open source .NET library for parsing scraped prices. Save time tuning your XPath and CSS selectors to get amount and currency accuratedly parsed out of raw string texts.
This project is based on the open source Python library price-parser
, by ScrapingHub.
- Prediction of thousands and decimal separator, if not specified.
- Support for +150 currencies.
- Currency hint specification as an alternate source for currency lookup.
Price parsedPrice = Price.FromString("Price: $119.00");
double? amount = parsedPrice.Amount // 119.00
string currency = parsedPrice.Currency // "$"
Price price = Price.FromString("Price: $119.00")
// price.Amount = 119.00; price.Currency = "$"
Price price = Price.FromString("15 130 Р")
// price.Amount = 15130; price.Currency = "Р"
Price price = Price.FromString("151,200 تومان")
// price.Amount = 151200; price.Currency = "تومان"
Price price = Price.FromString("Rp 1.550.000")
// price.Amount = 15500000; price.Currency = "Rp"
Price price = Price.FromString("Běžná cena 75 990,00 Kč")
// price.Amount = 75990; price.Currency = "Kč"
If either the amount or the currency are not found, the corresponding property for that price is set to null
.
Price price = Price.FromString("Foo")
// price.Amount = null; price.Currency = null
Price price = Price.FromString("Free")
// price.Amount = 0.0; price.Currency = null
Price price = Price.FromString("40% OFF")
// price.Amount = null; price.Currency = null
An alternate source string can be specified to try to extract the currency, if the latter could not be found in the provided raw price:
Price price = Price.FromString("8.29", rawCurrencyHint: "£ 8.29");
// price.Amount = 8.29; price.Currency = "£"
Price price = Price.FromString("8.29 EUR", rawCurrencyHint: "£ 8.29");
// price.Amount = 8.29; price.Currency = "EUR"
The library tries to predict both decimal and thousands separators but if this information is known beforehand, you can specify the style that is used:
Price price = Price.FromString("Price: $140.600", EDecimalSeparatorStyle.American)
// price.Amount = 140.6; price.Currency = "USD"
Price price = Price.FromString("Price: $140.600", EDecimalSeparatorStyle.European)
// price.Amount = 140600; price.Currency = "USD"
Price price = Price.FromString("Price: $140.600")
// price.Amount = 140600; price.Currency = "USD"
Note. The library assumes American and European styles use dots and commas as decimal separators, respectively.
The library is tested with +900 NUnit tests, which can be easily run from Visual Studio.
If this project helps you reduce development time, you may consider giving me a cup of coffee :-)