I want to find which Currency Symbol exists in Currency Format data.
For example, Input String = $56.23
public class FormatConverter
{
private CultureInfo _cultureInfo;
public void UpdateCultureInfo()
{
Thread.CurrentThread.CurrentCulture.ClearCachedData();
var thread = new Thread(
s => _cultureInfo = Thread.CurrentThread.CurrentCulture);
thread.Start();
thread.Join();
}
Bool TryParseCurrencySymbolAndValue(string input, out string CurrencySymbol,
out double value)
{
if(_cultureInfo == null)
UpdateCultureInfo();
try{
// Convert Currency data into double
value = Double.Parse(input, NumberStyles.Number | NumberStyles.AllowCurrencySymbol);
// How to extract Currency Symbol?
CurrencySymbol = "$";
return true;
}
catch(Exception ex){ /* Exception Handling */}
return false;
}
}
I want to extract "$" symbol from a string and 56.23 separately and then I want to apply CultureInfo to 56.23 into French Format. The output should be $56,23.
In some cases, input might be "Euro sign" or some other currency symbol in the beginning or in the end of input string.
I know how to convert into CurrentCulture for Numeric part. I don't know how to extract currency Symbol from a string.
It sounds like you already know how to parse the string into a number type (correct me if I'm wrong). You're using double in your example, I would suggest decimal but that's your choice.
To get the currency symbol you can use a simple regular expression
Regex ex = new Regex(#"\p{Sc}");
CurrencySymbol = ex.Match(input).Value;
I hope that helps.
Look at this link as well to give you and idea as to the many different ways you can find and or use IndexOf
[IndexOf String Examples][1]
the question is will the format always have the $ as the first char..? if the answer is yes
regardless of USC or Foreign Currency use the String.IndexOf Method
String.IndexOf("$")
here is a coded example that you may look at
using System;
class Program
{
static void Main()
{
// A.
// The input string.
const string s = "Tom Cruise is an Idiot he should pay $54.95.";
// B.
// Test with IndexOf.
if (s.IndexOf("$") != -1)
{
Console.Write("string contains '$'");
}
Console.ReadLine();
}
}
Output
string contains '$'
Can you please try with?
float curSymbol;
bool isValid = float.TryParse(curValue,
NumberStyles.Currency,
CultureInfo.GetCultureInfo("en-US"), out curSymbol);
Get the curSymbol. :) Be sure to pass currency values with symbol:)
Related
Hi I want to find if there is any better way to parse the string to Decimal which covers various format
$1.30
£1.50
€2,50
2,50 €
2.500,00 €
I see a lot of examples using culture to convert . & ,. But in my case, I don't have anything to identify the culture.
This display field I get from the client and I need to extract the value.
I tried following (which didn't work for all scenario) but would like to know if we have any best way to handle this.
Decimal.Parse(value,NumberStyles.Currency |
NumberStyles.Number|NumberStyles.AllowThousands |
NumberStyles.AllowTrailingSign | NumberStyles.AllowCurrencySymbol)
I also tried to use Regex to remove the currency sign but unable to convert both 1.8 or 1,8 in one logic.
Well, assuming you always get a valid currency format, and it's only the culture that changes, you could guess which character is used as a decimal point and which is used as a thousands separator by checking which appears the last in the number. Then remove all the thousand separators and parse it like its culture was invariant.
The code would look like the following:
// Replace with your input
var numberString = "2.500,00 €";
// Regex to extract the number part from the string (supports thousands and decimal separators)
// Simple replace of all non numeric and non ',' '.' characters with nothing might suffice as well
// Depends on the input you receive
var regex = new Regex"^[^\\d-]*(-?(?:\\d|(?<=\\d)\\.(?=\\d{3}))+(?:,\\d+)?|-?(?:\\d|(?<=\\d),(?=\\d{3}))+(?:\\.\\d+)?)[^\\d]*$");
char decimalChar;
char thousandsChar;
// Get the numeric part from the string
var numberPart = regex.Match(numberString).Groups[1].Value;
// Try to guess which character is used for decimals and which is used for thousands
if (numberPart.LastIndexOf(',') > numberPart.LastIndexOf('.'))
{
decimalChar = ',';
thousandsChar = '.';
}
else
{
decimalChar = '.';
thousandsChar = ',';
}
// Remove thousands separators as they are not needed for parsing
numberPart = numberPart.Replace(thousandsChar.ToString(), string.Empty);
// Replace decimal separator with the one from InvariantCulture
// This makes sure the decimal parses successfully using InvariantCulture
numberPart = numberPart.Replace(decimalChar.ToString(),
CultureInfo.InvariantCulture.NumberFormat.CurrencyDecimalSeparator);
// Voilá
var result = decimal.Parse(numberPart, NumberStyles.AllowDecimalPoint | NumberStyles.Number, CultureInfo.InvariantCulture);
It does look a bit of complicated for a simple decimal parsing, but I think should do the work for all the input numbers you get or at least the most of them.
If you do this in some sort of loop, you might want to use compiled regex.
The problem here is that in one case . means decimal point but in other it is a thousnads separator. And then you have , as decimal separator. Clearly, it is impossible for the parser to "guess" what is meant, so the only thing you can do is to decide on some rules on how to handle which case.
If you have control over the UI the best approach would be to validate user input and just reject any value that can't be parsed with an explanation on which format is expected.
If you have no control over the UI, the second best option would be to check for some "rules" and then devise which culture is appropriate for that given input and try to run it through decimal.TryParse for that given culture.
For the given input you have, you could have the following rules:
input.StartsWith("$") -> en-US
input.StartsWith("£") -> en-GB
input.StartsWith("€") || input.EndsWith("€") -> de-DE
These could reasonably handle all cases.
In code:
static void Main(string[] args)
{
string[] inputs =
{
"$1.30",
"£1.50",
"€2,50",
"2,50 €",
"2.500,00 €"
};
for (int i = 0; i < inputs.Length; i++)
{
Console.Write((i + 1).ToString() + ". ");
if (decimal.TryParse(inputs[i], NumberStyles.Currency,
GetAppropriateCulture(inputs[i]), out var parsed))
{
Console.WriteLine(parsed);
}
else
{
Console.WriteLine("Can't parse");
}
}
}
private static CultureInfo GetAppropriateCulture(string input)
{
if (input.StartsWith("$"))
return CultureInfo.CreateSpecificCulture("en-US");
if (input.StartsWith("£"))
return CultureInfo.CreateSpecificCulture("en-GB");
if (input.StartsWith("€") || input.EndsWith("€"))
return CultureInfo.CreateSpecificCulture("de-DE");
return CultureInfo.InvariantCulture;
}
Output:
1.30
1.50
2.50
2.50
2500.00
The only way you could do that is just strip string from symbols and change . and , to decimal separator. Something like:
public decimal UniversalConvertDecimal(string str)
{
char currentDecimalSeparator = Convert.ToChar(Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);
str = str.Replace('.', currentDecimalSeparator);
str = str.Replace(',', currentDecimalSeparator);
StringBuilder builder = new StringBuilder(str.Length);
foreach(var ch in str)
{
if(Char.IsDigit(ch) || ch == currentDecimalSeparator)
builder.Add(ch);
}
string s = builder.ToString();
return Convert.ToDecimal(s);
}
First you have to get current decimal separator from your system.
Then you have to replace . and , with current decimal separator.
Next, you will have to strip the string from any other char than a digit or decimal separator. At the end you can be sure that Convert.ToDecimal is going to work. But I don't know if it is something you want to achieve.
If you need some mechanism to save currency to database, there is a far simpler solution. Just convert this currency to least currency part. For example instead of $1, save 100 cents.
So if you have $1.99, just multiply it by 100 and you will get: 199 cents. And this integer can be saved to db.
Hi i need to convert a decimal value based on the culture info.
For EG :
If en-US culture the decimal value will be like 21.56 and in
tr-TR which is turkey culture info , here the same value will be as 21,56
Si my requirement is to whatever the decimal value , but i need to get in en-US as default. I need my decimal value seperated by dot and i dont want the decimal value of comma.
I tried converting using the below code
CultureInfo userCulture = new CultureInfo("tr-TR");
string fromVisioAPI = "35,2083";
string display = double.Parse(fromDb, userCulture).ToString();
and here the output was "35.2083" which was i expected, but here am hardcoding the tr-TR value and i dunno how many culture does the same comma and dot difference.
This was a normal replacement and i need to be a proper culture conversion
So what's the best way of converting comma decimal values to dot decimal values using culture info..?
You need to know the culture info for the data to parse. If that was static, you could simply hard code it or configure it in the app.config.
Otherwise you could use CultureInfo.CurrentCulture.
To convert the values back to string you should use the ToString overload with a specific culture info:
var visioApiCulture =
new CultureInfo(ConfigurationManager.AppSettings["VisioApiCulture"]);
or
var visioApiCulture = CultureInfo.CurrentCulture;
-
string fromVisioApi = "35,2083";
string display = double
.Parse(fromVisioApi, visioApiCulture)
.ToString(CultureInfo.InvariantCulture);
Assuming the Visio API is running on the same computer as your code, then the Windows culture info that Visio is using is very likely the same as what your program is getting as its default. You can get the current culture from the CultureInfo.CurrentCulture property: https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture(v=vs.110).aspx
It think the below code can help you ..
public double? ConvertStringToDouble(string strDoubleValue)
{
//Checking null
if (strDoubleValue == null)
{
return null;
}
//Making trim
strDoubleValue = strDoubleValue.Trim();
//Checking empty
if (strDoubleValue == string.Empty)
{
return null;
}
//If the amout treat dot(.) as decimal separator
if (strDoubleValue.IndexOf('.')!=-1)
{
//If multiple . is present then the amount is invaid
if (strDoubleValue.Count(o=>o=='.')>1)
{
return null;
}
//removing thousand separators
//it might not be needed
strDoubleValue = strDoubleValue.Replace(",", "");
return ConvertPlainStringToDouble(strDoubleValue);
}
//If the amout treat dot(,) as decimal separator
//then it must not use ',' as thousand separator
if (strDoubleValue.Count(o => o == ',') > 1)
{
//removing thousand separators
//it might not be needed
strDoubleValue = strDoubleValue.Replace(",", "");
return ConvertPlainStringToDouble(strDoubleValue);
}
//Here will be logic that the string contains single comma , is treated here as
//deciaml separator or comma separator
//int charCountBeforeComma = strDoubleValue.IndexOf(',');
//int charCountAfterComma = strDoubleValue.Length - (charCountBeforeComma + 1);
////If charCountAfterComma is not in 3rd position than
////the comma cannot be thousand separator example: 458,5896
//if (charCountAfterComma!=3)
//{
// //removing thousand separators
// //it might not be needed
// strDoubleValue = strDoubleValue.Replace(",", ".");
// return ConvertPlainStringToDouble(strDoubleValue);
//}
//if string having more than 3 char before comma like 4589,548
//it means no thousand separator used else the amount should represent like this 4,589,548
//you can use below code
//if (charCountBeforeComma>3)
//{
// //removing thousand separators
// //it might not be needed
// strDoubleValue = strDoubleValue.Replace(",", "");
// return ConvertPlainStringToDouble(strDoubleValue);
//}
//if all above missed than i am sorry
//it means the string is like 458,458 or 58,458 format
//you need to put some logical condition here
//??????
}
private Double? ConvertPlainStringToDouble(string strPlainDoubleValue)
{
Double amount;
if (Double.TryParse(strPlainDoubleValue, out amount))
{
return amount;
}
return null;
}
I tried to attend all logical condition to resolve the issue .. but in end .. there is something nee
d to fill by you .. :)
You should use the universal format properties in the API - those don't depend on the culture setting.
For example, to get the formula of a cell, use FormulaU - this will keep all the numbers in the . means decimal point, , means thousands separator format.
Using Result should also work, since it's supposed to return a double, not a string - if you're getting a string, either you're using an incorrect interop library, or you're doing something wrong. You can use ResultStrU as a work-around, again, to get an universal format string.
I'm a bit stuck: I am trying to perform an if statement comparing a label.text that originally has a currency string created from .ToString("C");
and a decimal. I have tried converting the label to decimal since I'm going to be comparing it to a decimal but it keeps triggering an exception:
Input string was not in a correct format.
here is my current code:
if(Convert.ToDecimal(SomeLabel.Text) > 1000.00m) { //DO SOMETHING }
//SomeLabel.Text has a value of $1000.00
//SomeLabel.Text has a value of $1000.00
The dollar sign is the reason for the issue. Yoa are probably using a different currency symbol. You can force it with decimal.Parse/decimal.TryParse(which handles invalid input):
string input = "$1000.00";
decimal decimalValue;
if(decimal.TryParse(input, out decimalValue))
{
// using current CurrencySymbol, same as Convert.ToDecimal
Console.WriteLine("Converted successfully: " + decimalValue);
}
else
{
var usCulture = new CultureInfo("en-US");
if (decimal.TryParse(input, NumberStyles.Currency, usCulture, out decimalValue))
{
// using dollar sign as CurrencySymbol
Console.WriteLine("Converted successfully with CultureInfo(en-US): " + decimalValue);
}
else
{
Console.WriteLine("Could not be parsed to decimal");
}
}
Output:
Converted successfully with CultureInfo(en-US): 1000.00
It's worth noting that NumberFormatInfo.InvariantInfo.CurrencySymbol does not return the dollar sign(what i thought) but ¤.
Convert.ToDecimal uses decimal.Parse explicitly this method uses NumberStyles.Number by default.
This is a composite style which includes AllowDecimalPoint but not AllowCurrencySymbol style even if your CurrentCulture's CurrencySymbol is $ and NumberDecimalSeparator is ..
You can use decimal.parse(String, NumberStyles, IFormatProvider) overlaod that takes these as a parameters like;
string s = "$1000.00";
var d = decimal.Parse(s, NumberStyles.AllowCurrencySymbol | NumberStyles.AllowDecimalPoint,
CultureInfo.GetCultureInfo("en-US"));
d will be 1000 after parsing.
As per your example. your string value is $1000.00.
This contains a $ sign that's why the compiler is unable to cast string to decimal.
try removing the unusable character by replace. Or try the method below
Regex digitsOnly = new Regex(#"[0-9.]"); // this regex matches only digits and decimal
string decimalnumberstring = digitsOnly.Replace(SomeLabel.Text, "");// replace all non numbers (except decimal) with empty string
then try to convert the string value like below.
if(Convert.ToDecimal(decimalnumberstring) > 1000.00m) { //DO SOMETHING }
hope it helps....
Take the Dollar Sign off of your text first:
Source: $1000
double result = 0M;
String value = SomeLabel.Text.Substring(1);
if (Double.TryParse(value, out result)) {
//
}
return result;
Try this
if(decimal.Parse(SomeLabel.Text) > 1000.00m)
{
}
I need to extract a variable length decimal number from a string using c# and .NET. The input string is like $PTNTHPR,352.5,N,2.3,N,4.6,N,16*. I need the first occurrence of decimal number, i.e the 352.5 part. The numerical value ranges from 0.0 to 360.0 and I need that number from that string.
I searched a lot and got solution for a fixed length sub string but here I have variable length to extract. I have not tried with any code yet.
If it is always in this format you can use String.Split and decimal.Parse
var data = #"$PTNTHPR,352.5,N,2.3,N,4.6,N,16*";
var d = decimal.Parse(data.Split(new[]{','})[1]);
Console.WriteLine(d);
This is just a sample code to guide you. You should add additional exception handling logic to this, Also consider using decimal.TryParse
If you want to find the first occurance of decimal value you split the string and parse them one by one.
var data = #"$PTNTHPR,352.5,N,2.3,N,4.6,N,16*";
var splited = data.Split(new[]{','});
decimal? value = null;
foreach (var part in splited)
{
decimal parsed;
if (decimal.TryParse(part, out parsed))
{
value = parsed;
break;
}
}
Console.WriteLine(value);
First occurence in any of the tokens? Use String.Split to separate them and LINQ to find the first. You can use decimal.TryParse to check if it's parsable:
decimal? firstParsableToken = "$PTNTHPR,352.5,N,2.3,N,4.6,N,16*".Split(',')
.Select(s => s.TryGetDecimal(NumberFormatInfo.InvariantInfo))
.FirstOrDefault(d => d.HasValue);
Used this simple extension method to parse it to decimal?:
public static decimal? TryGetDecimal(this string item, IFormatProvider formatProvider = null, NumberStyles nStyles = NumberStyles.Any)
{
if (formatProvider == null) formatProvider = NumberFormatInfo.CurrentInfo;
decimal d = 0m;
bool success = decimal.TryParse(item, nStyles, formatProvider, out d);
if (success)
return d;
else
return null;
}
If the string is always comma separated, can you not use string.Split() to get each section, then use double.TryParse() to test if that part is numeric?
public static class Helper
{
public static string MyExtract(this string s)
{
return s.Split(',').First(str => Regex.IsMatch(str, #"[0-9.,]"));
}
}
Use it like this: string str = "$PTNTHPR,352.5,N,2.3,N,4.6,N,16*".MyExtract();
Then convert it to double/decimal if you need it.
What is the best way to convert any currency into decimal in C#?
public static decimal returnDecimalFromCurrency(string dataToCheck) {
decimal varValue;
if (!Decimal.TryParse(dataToCheck, NumberStyles.Number | NumberStyles.AllowCurrencySymbol, CultureInfo.CurrentCulture, out varValue)) {
varValue = decimal.MinValue;
}
return varValue;
}
But this one does work for only my current currency. If i pass it EURO currency it won't get converted.
Or the only way is to always check what currency it is and pass proper CultureInfo:
CultureInfo.GetCultureInfo("pl-PL") for zl, or CultureInfo.GetCultureInfo("en-GB") for pounds?
Edit:
Or maybe this is the quick "working" hack?
Decimal.TryParse(dataToCheck.Trim().Replace("zł", "").Replace("€", ""), NumberStyles.Number | NumberStyles.AllowCurrencySymbol, CultureInfo.GetCultureInfo("pl-PL"), out varValue)
But to be honest I don't really like it.
I'd avoid trying to parse the currency with Decimal.TryParse and instead strip off the currency - giving the resultant string to TryParse. Deal with the currency separately - of course, create a function to do this if you need to do it more than once.
Here 's what works for me, I hope it helps you too:
public static decimal Parse(string currencyString)
{
return Parse(currencyString, Thread.CurrentThread.CurrentCulture);
}
public static decimal Parse(string currencyString, CultureInfo culture)
{
currencyString = currencyString.Replace(culture.NumberFormat.CurrencySymbol, System.String.Empty);
var value = Convert.ToDecimal(currencyString, culture);
return value;
}
I solved this same problem with a hybrid approach. The regex to ignore period, comma, parenthesis (in my case I know negatives are in parenthesis), and digits is fairly simple. Just use that to find your symbol, and then build a culture from it. Then parse.
//[^\d\.,()]+ all non-digit, non-period, non-comma, non-parenthesis chars
var regex = new Regex("[^\\d\\.,()]+");
var match = regex.Match(currencyString);
if(match == null){ //throw exception }
var currencySymbol = match.Value;
var cultureInfo = Thread.CurrentThread.CurrentCulture.Clone() as CultureInfo;
cultureInfo.NumberFormat.CurrencySymbol = currencySymbol;
var decimalValue = Decimal.Parse(currencyString, NumberStyles.Currency, cultureInfo);
I ran into this issue today, and I ended up just looping through all of the cultures, and passing each one to the TryParse function.
var myString = "$400.01";
var result = 0.0m;
var culture = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures)
.First(c => decimal.TryParse(myString, System.Globalization.NumberStyles.Currency, c, out value))
// result = 400.01m