Skip to content

Open source .NET Standard 2.0 library for parsing prices from raw strings

License

Notifications You must be signed in to change notification settings

tteguayco/dotnet-price-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dotnet-price-parser

The latest release can be downloaded via NuGet.

Open Source Love Awesome

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.

Features

  • Prediction of thousands and decimal separator, if not specified.
  • Support for +150 currencies.
  • Currency hint specification as an alternate source for currency lookup.

Usage

C#

Examples

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č"

Amount or currency not found

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

Special cases

Price price = Price.FromString("Free")
// price.Amount = 0.0; price.Currency = null
Price price = Price.FromString("40% OFF")
// price.Amount = null; price.Currency = null

Currency hint

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"

Decimal separator style

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.

Running the tests

The library is tested with +900 NUnit tests, which can be easily run from Visual Studio.

Donation

If this project helps you reduce development time, you may consider giving me a cup of coffee :-)

Donate

About

Open source .NET Standard 2.0 library for parsing prices from raw strings

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages