How do I convert string to Indian Money format? - c#

I am trying to convert string to India Money format like if input is "1234567" then output should come as "12,34,567"
I have written following code but its not giving the expected output.
CultureInfo hindi = new CultureInfo("hi-IN");
string text = string.Format(hindi, "{0:c}", fare);
return text;
can anyone tell me how to do this?

If fare is any of int, long, decimal, float or double then I get the expected output of:
₹ 12,34,567.00.
I suspect your fare is actually a string; strings are not formatted by string.Format: they are already a string: there is no value to format. So: parse it first (using whatever is appropriate, maybe an invariant decimal parse), then format the parsed value; for example:
// here we assume that `fare` is actually a `string`
string fare = "1234567";
decimal parsed = decimal.Parse(fare, CultureInfo.InvariantCulture);
CultureInfo hindi = new CultureInfo("hi-IN");
string text = string.Format(hindi, "{0:c}", parsed);
Edit re comments; to get just the formatted value without the currency symbol or decimal portion:
string text = string.Format(hindi, "{0:#,#}", value);

String.Format("0:C0") for no decimal places.
As per my comment above you can achieve what you desire by cloning a numberformatinfo and set the currency symbol property to empty string
Example can be found here - look down the bottom of the page
EDIT: Here is the above linked post formatted for your question:
var cultureInfo = new CultureInfo("hi-IN")
var numberFormatInfo = (NumberFormatInfo)cultureInfo.NumberFormat.Clone();
numberFormatInfo.CurrencySymbol = "";
var price = 1234567;
var formattedPrice = price.ToString("0:C0", numberFormatInfo); // Output: "12,34,567"

Try this
int myvalue = 123456789;
Console.WriteLine(myvalue.ToString("#,#.##", CultureInfo.CreateSpecificCulture("hi-IN")));//output;- 12,34,56,789

If you want to show in Razor view file, then use,
#String.Format(new System.Globalization.CultureInfo("hi-IN"), "{0:c}", decimal.Parse("12345678", System.Globalization.CultureInfo.InvariantCulture))
// Output: ₹ 1,23,45,678.00

Related

Convert string to decimal but have swedish currency

My code:
public decimal SetHourRate(string hourrate)
{
var value = decimal.Parse(hourrate, NumberStyles.Currency, CultureInfo.CurrentCulture.NumberFormat);
return value;
}
In parameter:
string hourrate = "800.00"
My problem:
Right now my output is like var value = 80000? I want it to be like the string I put in. I'm from Sweden and we have a dot instead of comma to separate the decimals in currency. It must work even if the string look like this string hourrate = "1050.67".
It's ok if the output will be like var value = "800,00" or var value ="1050,67"(comma instead of dot.)
It is returning the 80000 because the . in the swedish is the GroupSeparator. The , is the decimal separator.
You are passing a string like 800.00 so, . will be used as group separator. That is the reason you are getting this value.
You could force the decimal separator to be . chaging the a formater (CultureInfo), for sample:
public decimal SetHourRate(string hourrate)
{
var swedishCulture = new CultureInfo("sv-SE");
swedishCulture.NumberFormat.NumberDecimalSeparator = ".";
var value = decimal.Parse(hourrate, NumberStyles.Currency, swedishCulture);
return value;
}
You have to use CultureInfo to specify what formatting to use when parsing.
CultureInfo culture = new CultureInfo("sv-SE");
culture.NumberFormat.NumberDecimalSeparator = ".";
decimal value;
if (decimal.TryParse(hourrate, NumberStyles.Currency, culture, out value))
{
// Do what you want with value if successfully parsed
}
else
{
// Failed to parse
}
Make sure you include System.Globalization at the top of your code.

Convert decimal currency to comma separated value

I get from a webservice the following strings:
12.95
or
1,200.99
Is there an option to convert these values to the following values without manipulating the string?
12,95
or
1200,99
I tried it with some Culture options but didn't get it right...
EDIT
I tried this:
//return string.Format( "{0:f2}", Convert.ToDecimal( price ) );
//return string.Format(CultureInfo.GetCultureInfo("de-de"), "{0:0}", price);
NumberFormatInfo format = new System.Globalization.NumberFormatInfo();
format.CurrencyDecimalDigits = 2;
format.CurrencyDecimalSeparator = ",";
format.CurrencyGroupSeparator = "";
return decimal.Parse(price).ToString(format);
var input = "1,200.99";
//Convert to decimal using US culture (or other culture using . as decimal separator)
decimal value = decimal.Parse(input, CultureInfo.GetCultureInfo("en-US"));
//Convert to string using DE culture (or other culture using , as decimal separator)
string output = value.ToString(CultureInfo.GetCultureInfo("de-DE"));
Console.WriteLine(output); //1200,99
What about something like this:
double number;
double.TryParse("1,200.99", NumberStyles.Any, CultureInfo.CreateSpecificCulture("en-US"), out number);
var formattedNumber = number.ToString(CultureInfo.CreateSpecificCulture("de-DE"));
Then return or write out formattedNumber (whatever you need to do).
Yes and no. First, what you have is a string, and so you cannot change the formatting of it as you're attempting to. However, to achieve what you would like, you can parse the string into a decimal value and then use the formatting options for decimals to display it in any reasonable way.
You may try for something like this:
String.Format("{0:#,###0}", 0);
or may be like this:
string str = yourNumber.Remove(",").Replace(".",",");
Close enough tronc,
Try this snippet:
String curStr = "12.95";
Decimal decVal;
var valid = Decimal.TryParse(curStr, out decVal);
if (!valid) throw new Exception("Invalid format.");
String newFormat = decVal.ToString("C", System.Globalization.CultureInfo.CreateSpecificCulture("de-DE"));
Within the toString(...) call, you can append a number after 'C' to specify how many decimal places should follow. E.g "C3".

double.parse convert two zero decimal to one decimal

I have a string 10.00 and I want to convert it to double 10.00.
I use :
string str = "10.00";
double db = double.Parse(str);
the result I get is 10.0 and not 10.00.
The Parse and TryParse on the numeric respect local culture settings; you can change this by specifying a CultureInfo object. For instance, parsing 2.999 into a double gives 2999 in Germany:
Console.WriteLine (double.Parse ("2.999")); // 2999 (In Germany)
This is because in Germany, the period indicates a thousands separator rather than a decimal point. Specifying invariant culture fixes this:
double x = double.Parse ("2.999", CultureInfo.InvariantCulture);
The same when calling ToString():
string x = 2.9999.ToString (CultureInfo.InvariantCulture);
A double isn't a string. If you want to display the double as a string, you can format it to have two decimal points.
For example:
string str = "10.00";
double db = double.Parse(str);
String.Format("{0:0.00}", db); // will show 10.00
Question isn't really clear, but if you are referring to changing the double back to string with 2 decimal place precision, you can use:
string str = "10.00"
double db = double.parse(str);
string convertedBack = db.ToString("0.00");

Changing the number of integers on a output value after the decimal point

So I'm learning and practicing WP7 application development.
I'm working with integers (currency), and it seems to always display four integers after the decimal place. I'm trying to cut it down to just either ONE or TWO decimal places.
I've been trying to use the "my variable.ToString("C2")" (C for Currency, 2 for number of ints after the decimal)
I'm probably missing something obvious, but please help
decimal number = new decimal(1000.12345678);
string text = number.ToString("#.##");
Output:
1000,12
An other way:
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.CurrencyDecimalDigits = 2;
decimal val = new decimal(1000.12345678);
string text = val.ToString("c", nfi);
When formatting a currency, NumberFormatInfo allows specifying following properties as well:
CurrencyDecimalDigits
CurrencyDecimalSeparator
CurrencyGroupSeparator
CurrencyGroupSizes
CurrencyNegativePattern
CurrencyPositivePattern
CurrencySymbol
See Custom Numeric Format Strings on MSDN for more examples
The "C" format string defines the currency specifier as described on MSDN. This will include the currency symbol for the current culture, or for a specific culture if supplied, e.g.
double amount = 1234.5678;
string formatted = amount.ToString("C", CultureInfo.CreateSpecificCulture("en-US"));
// This gives $1234.56
In your case, it seems that you have a limited set of currency symbols that you support, so I would suggest using the fixed point format specifier "F" instead. By default this will give you 2 decimal points, but you can specify a number to vary this, e.g.
double amount = 1234.5678;
string formatted = amount.ToString("F");
// This gives 1234.56
formatted = amount.ToString("F3");
// This gives 1234.567
Using the fixed point specifier will give you control over the number of decimal points and enable you to concatenate the currency symbol.
The only thing I would add to "sll" answer is to pay attention on Culture (they often forget to mantion this), like this (example)
string text = val.ToString("#.##", CultureInfo.InvariantCulture);
double total = 526.4134
string moneyValue = total.ToString("c");
This will display it in this format: $#.##

How can I modify the currency formatter to allow negative money amounts in C#?

If I have something like:
Decimal moneyAmount = -1522;
and then call
moneyAmount.toString("c");
It will return something like:
($1,522)
Instead, I wish for there to be a negative sign and no paraenthesis. How can I modify what format provider I send to toString() so I can achieve the following effect:
-$1,522
Taken from: http://keithdevens.com/weblog/archive/2007/Jun/04/C-sharp.currency
// set currency format
string curCulture = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
System.Globalization.NumberFormatInfo currencyFormat = new System.Globalization.CultureInfo(curCulture).NumberFormat;
currencyFormat.CurrencyNegativePattern = 1;
number.ToString("c", currencyFormat);
// or string.Format(currencyFormat, "{0:c}", number);

Categories

Resources