How to Humanize currency with decimal values? #1538
-
|
hello all, I see the library having options to Humanize integer values. However I would like ot Humanize decimal values and I'm surprised to see no options available. Please can you assist me on how to handle it using this library? I'm not able to find anything from Readme on this. Is there any other docs which I'm missing ? Example, INR 123.10 should be Humanized to One Hundred Twenty Three Rupees and Ten Paise |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Hello! Currently, Humanizer focuses on humanizing types (integers, dates, enums) rather than specific financial currency patterns. To achieve the 'Rupees and Paise' format, the best approach is to humanize the integer and fractional parts separately. You can achieve this with the following approach (or similar): using Humanizer;
public string HumanizeCurrency(decimal amount)
{
// 1. Get the main units (Rupees)
int rupees = (int)Math.Truncate(amount);
// 2. Get the subunits (Paise), rounding it to 2 decimal places
int paise = (int)((amount - rupees) * 100);
// 3. Humanize both parts
string rupeesPart = rupees.ToWords();
string paisePart = paise.ToWords();
// 4. Combine and use .Transform(To.TitleCase) to match your requirement
string result = $"{rupeesPart} Rupees and {paisePart} Paise".Transform(To.TitleCase);
return result;
}Example: 123.10m -> "One Hundred Twenty Three Rupees And Ten Paise" |
Beta Was this translation helpful? Give feedback.
Hello!
Currently, Humanizer focuses on humanizing types (integers, dates, enums) rather than specific financial currency patterns. To achieve the 'Rupees and Paise' format, the best approach is to humanize the integer and fractional parts separately.
You can achieve this with the following approach (or similar):