Reputation: 35
I am getting decimal values in Webservice with comma.
Eg: 0,000 | 12,21365659697|11,125487654.
What i want is string but only with 2 values after comma.
I tried
Initilize-
decimal val=0m;
decimal val2=0m;
Getting data from webservice and mapping to my field:
"val": val = Convert.ToDecimal(GETDATA.GETDATA[0].GETDATA[i].GETDATA.N_VALUE);
And sending this value to my SQL with column defined as varchar after converting it to string .
public static string getdecimalSqlString(decimal decvar)
{
return decvar.ToString("#,##", CultureInfo.InvariantCulture);
}
I assume this works well but my problem is if i do not get value ,then if I convert val2 with given condition,it will fail.
Also,if i use nullable decimal to initilize,i can not use formatting .
Thanks
P.S.-I may or may not get decimal(comma instead of decimal) and need string instead of decimal but with only 2 places after comma)
Approach1: is this right approach ?
return decvar.ToString("#.##", new CultureInfo("en-US", true));
Approach2 Or this one?
public static string getdecimalSqlString(decimal? decvar1)
{
string a= decvar1.HasValue? decvar1.Value.ToString("#,##") : string.Empty;
//return decvar.ToString("#.##", new CultureInfo("en-US", true));
return a;
}
Upvotes: 0
Views: 2271
Reputation: 124696
A Web Service that returns a decimal value with a comma separator is a red flag. Often it means the developer of the web service has formatted the string using the CurrentCulture
, which happens to be a culture that has comma as a decimal separator. And if so, moving the service to a server with a different culture may change the decimal separator it returns.
Therefore I would start by trying to confirm with the developer of the Web Service whether it is intentionally returning a comma separator, or whether it should be an invariant period separator.
To get the decimal value, you need to parse using the appropriate culture (Culture.InvariantCulture
is probably best for a period separator, and an example of a culture that uses a comma separator is new CultureInfo("fr-FR")
:
Decimal.TryParse(value, NumberStyles.Number, myCulture, out result)
Once you have the decimal value, you can round to two decimal places using Math.Round
.
Your code in getDecimalSqlString is wrong:
return decvar.ToString("#,##", CultureInfo.InvariantCulture);
The format specifier should be "#.##" and if you want a comma separator you should specify a culture that uses one.
Finally, you say
And sending this value to my SQL with column defined as varchar after converting it to string
In most cases, you should not be converting decimal to string before using it in an SQL query. Instead, you should use a parameter with a decimal value in your query.
Upvotes: 2
Reputation: 8841
Maybe this will Helpful
public decimal _MFormatNo(string _value, int _pFractions = 2)
{
decimal _pvalue = Convert.ToDecimal (_value.Remove(','));
string _Format = ".";
string Value = "";
for (int i = 0; i < _pFractions; i++)
{
_Format += "0";
}
if (_pvalue == 0)
{
Value = "0" + _pvalue.ToString(_Format);
}
Value = (_pvalue).ToString(_Format);
return Convert.ToDecimal(Value);
}
UPDATE : As per OP requirement as she want string output
public string _MFormatNo(string _value, int _pFractions = 2)
{
decimal _pvalue = Convert.ToDecimal(_value.Remove(','));
string _Format = ".";
string Value = "";
for (int i = 0; i < _pFractions; i++)
{
_Format += "0";
}
if (_pvalue == 0)
{
Value = "0" + _pvalue.ToString(_Format);
}
Value = (_pvalue).ToString(_Format);
return Value;
}
Upvotes: 0