Unnusual String convertion result from string to decimal.

May 20 2008 9:58 AM

Hello

 

 

I have a xml node that gives me the following value 4,2, I then convert it to decimal, I use it to make some calculations, I then use the string again to replace the "," for "." and I add zeros to the left and right of the "." and the result should be something like:

 

000000004.20, instead I get 000000004.22, where the heck those decims appeared? it happens again to another value 16,80 it apperars again 000000016.88 it should appear 000000016.80, at present it happens only for those values, there are other string transformations that go correctly but those two values (so far) don't to me this is odd, I even used system.globalization.

 

Code:

 

pUnit = string.Empty;

pUnit = xmldoc.SelectSingleNode("/Scriptor/ContentList/ContentData[" + i + "]/Ocasiao_Encomendas/Ocasiao_Encomendas_Parts/ci_pagamentos/scrExpandedContent[" + v + "]/Ocasiao_Pagamentos/Ocasiao_Pagamentos_Parts/txt_adValue").InnerText;

pUnit = pUnit.Replace(".", ",");

 

....

 

 

/////////////////Some calculations

 

 

......

 

 

 

 

pUnit = pUnit.Replace(",", ".");

NumberFormatInfo NFormat = new NumberFormatInfo();

NFormat.NumberDecimalDigits = 2;

pUnit = Convert.ToString(arredDec).ToString(NFormat);

 

string pUnit_temp1 = "";

string pUnit_temp2 = "";

string[] pUnitArray;

int pUnit1 = 0;

int pUnit2 = 0;

//int pUnit3 = 0;

pUnitArray = pUnit.Split(',');

 

 

if (pUnitArray.Length == 1)

{

pUnit1 = pUnitArray[0].Length;

pUnit2 = 0;

pUnit_temp2 = "";

}

if (pUnitArray.Length == 2)

{

pUnit1 = pUnitArray[0].Length;

pUnit2 = pUnitArray[1].Length;

pUnit_temp2 = pUnitArray[1];

}

 

 

if (pUnit2 == 0)

{

for (int j = pUnit1; j < 9; j++)

{

pUnit_temp1 += "0";

}

pUnit = pUnit_temp1 + pUnit + ".00";

}

 

if ((pUnit2 >= 1) && (pUnit2 < 2))

{

for (int j = pUnit1; j < 9; j++)

{

pUnit_temp1 += "0";

}

pUnit = pUnit_temp1 + pUnit;

for (int j = pUnit2; j < 1; j++)

{

pUnit_temp2 += "0";

}

pUnit = pUnit + pUnit_temp2;

}

 

if ((pUnit2 >= 1) && (pUnit2 == 2))

{

for (int j = pUnit1; j < 9; j++)

{

pUnit_temp1 += "0";

}

pUnit = pUnit_temp1 + pUnit;

}

 

 


Answers (1)