Double parsing problem in C sharp
SeKo
Hi all,I'm having a problem with C-Sharp, I know it's not directly related to SAP B1, but I still hope someone can help me
An add-on we're building in Coresuite will need to be used on different servers worldwide. That means that the localization of the servers can be different.
Now we would like to parse a double: 1234.56
In SAP it is defined that the decimal separator is a point, in the OS of the server, it is defined that the decimal separator is a comma.
That means that if I get a string from a matrix: 1234.56 (since SAP uses a point) and then use double.Parse(amount) I get 123456 sine the system uses a comma.
I can get the decimal point (and other info) out of the OADM table, but I don't know how to tell the double.Parse method to use a specific format to read the string.
Also, replacing comma's with points will not resolve anything: 1,234,567.89 is also a possible value.
Did anyone encounter this problem or now how to resolve it?
Thanks!
Sebastiaan
Friederike Mundt
Hi Sebastian,Not sure if this helps. I had some issues transferring a value 1.00 to compare with a double value in different systems.
I used the following code which worked:
Convert.ToDouble(1.00, System.Globalization.CultureInfo.InvariantCulture);
In your case it seems the other way around and the SAP settings might be different than the System settings so you might have to replace all special characters and add the decimal separator at the correct place.
Maybe it helps.
Cheers,
Friederike
SeKo
Go it! Here is the code I use in my extended code. The D2S and the S2D convert strings and doubles by using the decimal separator from SAP, and not the one from windows. I also use a GetQueryResult(string query) to make getting small things out of the database easier.
static System.Globalization.NumberFormatInfo ni = null;
static string decsep=null;
private string GetDecSep(){
if (decsep == null){
decsep = GetQueryResult("select decsep from OADM")[0];
}
return decsep;
}
//Double To String converter, using SAP DECSEP
private string D2S(double number){
string nr = "";
try{
if (decsep == null){
decsep = GetQueryResult("select decsep from OADM")[0];
}
nr = String.Format("{0:0" + GetDecSep() + "000000}", number);
nr = number.ToString(GetNi());
}catch(Exception ex){
MessageBox.Show(ex.ToString(), "ok");
}
return nr;
}
private System.Globalization.NumberFormatInfo GetNi(){
if(ni == null){
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.InstalledUICulture;
ni = (System.Globalization.NumberFormatInfo) ci.NumberFormat.Clone();
ni.NumberDecimalSeparator = GetDecSep();
}
return ni;
}
//String To Double converter, using SAP DECSEP
private double S2D(string number)
{
double nr = 0;
try{
nr = double.Parse(number, GetNi());
}catch(Exception ex){}
return nr;
}
private string[] GetQueryResult(string query){
string[] res = null;
try{
using (System.Data.SqlClient.SqlDataReader rdr = SwissAddonFramework.B1Connector.GetB1Connector().ExecuteQuery(query))
{
rdr.Read();
if (rdr.HasRows){
res = new string[rdr.FieldCount];
for (int i = 0;i < rdr.FieldCount;i++){
res = rdr.GetString(i);
}
}
rdr.Close();
}
}catch(Exception ex){
MessageBox.Show(ex.ToString(), "ok");
}
return res;
}
berbericz
Hi there and thank you for posting and solving that format conversion issues with SAP B1/Coresuite and the local OS.We also use a Thousands separator and not only a separator for decimals. I modified coding and added new methods.
[CODE]// new Thousands Sep.
private string GetThousSep(){
if (thoussep == null){
thoussep = GetQueryResult("select thoussep from OADM")[0];
}
return thoussep;
}[/CODE]
Our Decimal Separator is a comma (,) and our thousands Separator is a dot (.).
So in weight-field or price-fields I have the value of 1.044,000 for example.
How can I convert String to Double (S2D) by your logic?
[CODE]private System.Globalization.NumberFormatInfo GetNi(){
if(ni == null){
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.InstalledUICulture;
ni = (System.Globalization.NumberFormatInfo) ci.NumberFormat.Clone();
MessageBox.Show("Format: " +GetNi(),"OK");
ni.NumberDecimalSeparator = GetDecSep();
//new
ni.NumberGroupSeparator = GetThousSep();
}
return ni;[/CODE]
At the moment this does not work! Any ideas?
Thanks and best regards
0
Please sign in to leave a comment.
Comments
0 comments