Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Currency to string

Tags:

c#

.net

currency

Normally I would use string.Format() to get a formatted string.

But I got the requirement that all amounts should be printed as text and not digits.

i.e. something like:

Format(3000, "us"); // => Resulting text: "three thousand dollars"

Are there a .NET library which can handle that (russian is mandatory)?

like image 705
jgauffin Avatar asked Nov 01 '12 08:11

jgauffin


People also ask

How do I convert currency to string?

string value = (Convert. ToDecimal(ReturnValue)). ToString("c");

What is the ToString format code for currency?

The "C" (or currency) format specifier is used to convert a number to a string representing a currency amount. Let us see an example. double value = 139.87; Now to display the above number until three decimal places, use (“C3”) currency format specifier.

How do you convert string to currency in Python?

To format numbers as currency in Python, the easiest way is with the locale module. You can also use the string format() function. Finally, you can use the babel. numbers module to format numbers as money and currency.


2 Answers

Are there a .NET library which can handle that (russian is mandatory)?

Short answer:No.

You have to write your own, I would recommend you to have a look at the following though: converting numbers in to words C# , How can I convert an integer into its verbal representation? and as one of these says, have a look at project Euler problem number 17 and use it for wider google searches.

On top of that you have the issue of currency names, should 'us' denote the currency or the language or even both? Is the canadian dollar different from the american dollar?

For example this is built in:

// Gives USD
var ISOCode = System.Globalization.RegionInfo.RegionInfo("US").ISOCurrencySymbol
// Gives $
var symbol = System.Globalization.RegionInfo.RegionInfo("US").CurrencySymbol
// Gives USD Dollar (i believe :))
var nameUSDENG = System.Globalization.RegionInfo.RegionInfo("US").CurrencyEnglishName
// Gives Svensk Krona
var nameSEKSWE = System.Globalization.RegionInfo.RegionInfo("SE").CurrencyNativeName
// Gives Swedish Krona
var nameSEKENG = System.Globalization.RegionInfo.RegionInfo("SE").CurrencyEnglishName

I would suggest you to start there and see where the path takes you.

like image 136
flindeberg Avatar answered Sep 22 '22 05:09

flindeberg


There is a mature library for doing with you needd. It's called humanizer.

You can see what it is about here: NuGet Package of the Week: Humanizer makes .NET data types more human

The open source project is on GitHub

As you can see it does what you need and many more things. And it has a lot of localizations.

Some examples:

1.ToWords() => "one"
10.ToWords() => "ten"
11.ToWords() => "eleven"
122.ToWords() => "one hundred and twenty-two"
3501.ToWords() => "three thousand five hundred and one"

You can also pass a second argument, GrammaticalGender, to ToWords to specify which gender the number should be outputted in.

1.ToWords(GrammaticalGender.Masculine) => "один"
1.ToWords(GrammaticalGender.Feminine) => "одна"
1.ToWords(GrammaticalGender.Neuter) => "одно"
like image 31
JotaBe Avatar answered Sep 22 '22 05:09

JotaBe