reinvest a 2-digit number

18/01/2010 - 19:15 por fred | Informe spam
hello:
I need to know how to reinvest a 2-digit number but that number is of type
double, for the integers I works well, but not the other.



ex:
34,53 change to:
43,53
 

Leer las respuestas

#1 Angel J. Hernandez M.
31/01/2010 - 01:02 | Informe spam
Hi mate,

You can do it by implementing an extension class/method for the double type,
as shown below:

public static class doubleExtender {
public static double Reinvert(this double valueToReinvert) {
int index = 0;
double retval = 0.0;
string[] tempBuffer = null;
string[] reinverted = null;

if (valueToReinvert != 0) {
tempBuffer = valueToReinvert.ToString().Split('.');
reinverted = new string[tempBuffer[0].Length];

for (int nChar = reinverted.Length - 1; nChar >= 0; nChar--) {
reinverted[index] += tempBuffer[0][nChar];
index++;
}
retval = double.Parse(string.Format("{0}.{1}", new object[]
{string.Concat(reinverted), tempBuffer[1]}));
}
return retval;
}
}


class Program {
static void Main(string[] args) {
double number = 34.53;

Console.WriteLine("Original Value:{0}", number);
Console.WriteLine("New Value:{0}", number.Reinvert()); // Call your
extension method here

Console.ReadLine();


}
}


Regards,


Angel J. Hernandez M.
MCP,MCAD,MCSD,MCDBA
Microsoft MVP

http://twitter.com/angeljesus14
http://msmvps.com/blogs/angelhernandez

"fred" wrote in message
news:
hello:
I need to know how to reinvest a 2-digit number but that number is of type
double, for the integers I works well, but not the other.



ex:
34,53 change to:
43,53

Preguntas similares