How to convert double value into string value - c#

I want to convert a double value into a string value, but when I am concatenating this string(double) value, it is changing the . to , and I can't manipulate this value. I tried to use String.Replace but didn't work too.
What I can do in this case. Here is my Code.
object[] campos = new object[1];
campos[0] = (double)56.25566;
Parameters[1] = "gdinvdllo005.start.load.coleta.o(" + campos[0].ToString() + ")";

It sounds like your thread has a culture which uses "," as the decimal separator. The simplest approach is probably to call string.Format specifying the invariant culture:
Parameters[1] = string.Format(
CultureInfo.InvariantCulture,
"gdinvdllo005.start.load.coleta.o({0})",
campos[0]);

It seems that your current culture uses a comma as decimal separator but you want to force a point. Then you can use NumberFormatInfo.InvariantInfo in double.ToString:
double doubleValue = 56.25566;
string stringValue = doubleValue.ToString(NumberFormatInfo.InvariantInfo);
Parameters[1] = "gdinvdllo005.start.load.coleta.o(" + stringValue + ")";

Related

How to changes "15.49" in string array , int values in c#

I have a question. how to changes "15.49" in string array to int value in c#?
Based on your comment, your culture uses "," as decimal separator, instead of ".". You could use overload of decimal.Parse to use custom culture format. Since you want to ignore the decimal part, you can use Int32.Convert to fetch the int part.
var stringValue = "15,49";
var culture = (CultureInfo)CultureInfo.InvariantCulture.Clone();
culture.NumberFormat.NumberDecimalSeparator = ",";
culture.NumberFormat.NumberDecimalSeparator = ",";
var result = Convert.ToInt32( decimal.Parse(stringValue, culture));
This would give your the required result
(int)decimal.Parse("15.49"); - if string null, parse return exception (maybe its what you want)
(int)Convert.ToDecimal("15.49"); if string null, convert return 0
If i am not wrong you want to convert string "15.49" into int ??? If yes then you can use this code Convert.toInt32("15.49");

C# Visual Studio 2015 using Math.Pow gives different outputs [duplicate]

I have two nvarchar fields in a database to store the DataType and DefaultValue, and I have a DataType Double and value as 65.89875 in English format.
Now I want the user to see the value as per the selected browser language format (65.89875 in English should be displayed as 65,89875 in German). Now if the user edits from German format to 65,89875 which is 65.89875 equivalent in English, and the other user views from an English browser it comes as 6589875.
This happens because in the database it was stored as 65,89875 in the nvarchar column and when converted using English culture it becomes 6589875 since it considers , as a separator which is a decimal operator for German.
How do I get this working for all the browsers?
You need to define a single locale that you will use for the data stored in the database, the invariant culture is there for exactly this purpose.
When you display convert to the native type and then format for the user's culture.
E.g. to display:
string fromDb = "123.56";
string display = double.Parse(fromDb, CultureInfo.InvariantCulture).ToString(userCulture);
to store:
string fromUser = "132,56";
double value;
// Probably want to use a more specific NumberStyles selection here.
if (!double.TryParse(fromUser, NumberStyles.Any, userCulture, out value)) {
// Error...
}
string forDB = value.ToString(CultureInfo.InvariantCulture);
PS. It, almost, goes without saying that using a column with a datatype that matches the data would be even better (but sometimes legacy applies).
You can change your UI culture to anything you want, but you should change the number separator like this:
CultureInfo info = new CultureInfo("fa-IR");
info.NumberFormat.NumberDecimalSeparator = ".";
Thread.CurrentThread.CurrentCulture = info;
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
With this, your strings converts like this: "12.49" instead of "12,49" or "12/49"
Convert.ToDouble(x) can also have a second parameter that indicates the CultureInfo and when you set it to
System.Globalization.CultureInfo InvariantCulture
the result will allways be the same.
I took some help from MSDN, but this is my answer:
double number;
string localStringNumber;
string doubleNumericValueasString = "65.89875";
System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowDecimalPoint;
if (double.TryParse(doubleNumericValueasString, style, System.Globalization.CultureInfo.InvariantCulture, out number))
Console.WriteLine("Converted '{0}' to {1}.", doubleNumericValueasString, number);
else
Console.WriteLine("Unable to convert '{0}'.", doubleNumericValueasString);
localStringNumber =number.ToString(System.Globalization.CultureInfo.CreateSpecificCulture("de-DE"));
You can convert the value user provides to a double and store it again as nvarchar, with the aid of FormatProviders. CultureInfo is a typical FormatProvider. Assuming you know the culture you are operating,
System.Globalization.CultureInfo EnglishCulture = new System.Globalization.CultureInfo("en-EN");
System.Globalization.CultureInfo GermanCulture = new System.Globalization.CultureInfo("de-de");
will suffice to do the neccesary transformation, like;
double val;
if(double.TryParse("65,89875", System.Globalization.NumberStyles.Float, GermanCulture, out val))
{
string valInGermanFormat = val.ToString(GermanCulture);
string valInEnglishFormat = val.ToString(EnglishCulture);
}
if(double.TryParse("65.89875", System.Globalization.NumberStyles.Float, EnglishCulture, out val))
{
string valInGermanFormat = val.ToString(GermanCulture);
string valInEnglishFormat = val.ToString(EnglishCulture);
}
Use InvariantCulture. The decimal separator is always "." eventually you can replace "," by "."
When you display the result , use your local culture. But internally use always invariant culture
TryParse does not allway work as we would expect There are change request in .net in this area:
https://github.com/dotnet/runtime/issues/25868
I have this function in my toolbelt since years ago (all the function and variable names are messy and mixing Spanish and English, sorry for that).
It lets the user use , and . to separate the decimals and will try to do the best if both symbols are used.
Public Shared Function TryCDec(ByVal texto As String, Optional ByVal DefaultValue As Decimal = 0) As Decimal
If String.IsNullOrEmpty(texto) Then
Return DefaultValue
End If
Dim CurAsTexto As String = texto.Trim.Replace("$", "").Replace(" ", "")
''// You can probably use a more modern way to find out the
''// System current locale, this function was done long time ago
Dim SepDecimal As String, SepMiles As String
If CDbl("3,24") = 324 Then
SepDecimal = "."
SepMiles = ","
Else
SepDecimal = ","
SepMiles = "."
End If
If InStr(CurAsTexto, SepDecimal) > 0 Then
If InStr(CurAsTexto, SepMiles) > 0 Then
''//both symbols was used find out what was correct
If InStr(CurAsTexto, SepDecimal) > InStr(CurAsTexto, SepMiles) Then
''// The usage was correct, but get rid of thousand separator
CurAsTexto = Replace(CurAsTexto, SepMiles, "")
Else
''// The usage was incorrect, but get rid of decimal separator and then replace it
CurAsTexto = Replace(CurAsTexto, SepDecimal, "")
CurAsTexto = Replace(CurAsTexto, SepMiles, SepDecimal)
End If
End If
Else
CurAsTexto = Replace(CurAsTexto, SepMiles, SepDecimal)
End If
''// At last we try to tryParse, just in case
Dim retval As Decimal = DefaultValue
Decimal.TryParse(CurAsTexto, retval)
Return retval
End Function

Convert string to double using special separators

I can't figure out how to do the following :
I want to import some data from a file, including numeric values. The user can personalize separators, which are char. For exemple, a number may look like this : 2 524,2. Here, we have a "thousands" separator () and a "decimal" separator (,).
I try to convert these strings as double.
I know that I may do something like this :
double.Parse(str.Replace(tSep, '\0').Replace(dSep, '.'));
But I'm looking for a potential way of doing it more properly.
Thank you in advance.
Try this:
string s = "2 524,2";
CultureInfo ci = new CultureInfo(1);
NumberFormatInfo ni = new NumberFormatInfo();
ni.NumberGroupSeparator = " ";
ni.NumberDecimalSeparator = ",";
ci.NumberFormat = ni;
decimal d = decimal.Parse(s, ci);
double.Parse(str.Replace(' ', '\0').Replace(',', '.'));
is fine, but you should also set the culture to InvariantCulture
double.Parse(str.Replace(' ', '\0').Replace(',', '.',
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture));
in order to be sure that your code will work on every user culture setting

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".

Insert dot symbol into a string number

I have int variables, example:
int money = 1234567890;
How I can insert "." into money, and make its format like this:
1.234.567.890
You can simply do this:
var text = money.ToString("N0",
System.Globalization.CultureInfo.GetCultureInfo("de"));
The result is:
1.234.567.890
(I just picked the German culture as I knew they use . for the separator.)
You can use NumberFormatInfo.NumberGroupSeparator:
NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;
nfi.NumberGroupSeparator = ".";
Int64 myInt = 1234567890;
Console.WriteLine( myInt.ToString( "N", nfi ) );
(Link to ideone.)
To get exactly the format, use
int money = 1234567890;
money.ToString(#"#\.###\.###\.##0");
More information on custom formats here. You need to escape the dot because otherwise the first one will be interpreted as the decimal one. 0 in the end is necessary if you want to display it for zero values.
If you want a "Money' format try:
int money = 1234567890;
string moneyString = String.Format("{0:C}", money);
returns "$1,234,567,890.00"
Im not sure what money format uses '.' instead of ',' but that could just be a globalization thing.

Categories

Resources