ConvertToReadableNumber
-
Can anyone tell me what this code does? :-D I found it in a project I inherited:
public static string ConvertToReadableNumber(decimal num)
{
bool isNegative = num<0;
num = Math.Floor(num);
string numTxt = Math.Abs(num).ToString();
string numReadable = "";
int counter =0;
for (int i = numTxt.Length-1; i >=0; i--)
{
if (counter > 0 && counter % 3 == 0)
numReadable = "," + numReadable;
numReadable = numTxt[i] + numReadable;
counter++;
}
if(isNegative)
numReadable = "-"+numReadable;
return numReadable;
}I'm guessing that a ToString() would have done the job. :)
The real wtf is the enormous variation in the paramters of the conversion function. When you know how to do it correctly with VB6, it won't work with C++, C#, Oracle SQL, MySQL, Microsoft SQL Server, ... I guess the guy who wrote the code was a victim of that diversity, and wrote it after switching to C# from something else.
-
Can anyone tell me what this code does? :-D I found it in a project I inherited:
public static string ConvertToReadableNumber(decimal num)
{
bool isNegative = num<0;
num = Math.Floor(num);
string numTxt = Math.Abs(num).ToString();
string numReadable = "";
int counter =0;
for (int i = numTxt.Length-1; i >=0; i--)
{
if (counter > 0 && counter % 3 == 0)
numReadable = "," + numReadable;
numReadable = numTxt[i] + numReadable;
counter++;
}
if(isNegative)
numReadable = "-"+numReadable;
return numReadable;
}I'm guessing that a ToString() would have done the job. :)
ToString would have done a better job as it's not depending on the culture. Lot's of companies that have utility-functions like this in their "toobox", often rebuilding what's already there. To me, that proves that most of these companies are too rich. If they had to be careful on what they'd spend, shit like this would be gone.
Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]
-
Can anyone tell me what this code does? :-D I found it in a project I inherited:
public static string ConvertToReadableNumber(decimal num)
{
bool isNegative = num<0;
num = Math.Floor(num);
string numTxt = Math.Abs(num).ToString();
string numReadable = "";
int counter =0;
for (int i = numTxt.Length-1; i >=0; i--)
{
if (counter > 0 && counter % 3 == 0)
numReadable = "," + numReadable;
numReadable = numTxt[i] + numReadable;
counter++;
}
if(isNegative)
numReadable = "-"+numReadable;
return numReadable;
}I'm guessing that a ToString() would have done the job. :)
Arpikusz wrote:
numReadable = "," + numReadable;
What happens if the culture is one that uses . instead of , for the separator? Not only is this bad code, it's lazy bad code because it's not taking localisation into account. Also, as I look over the code, what happens if num is 3000.27? I can't see anywhere in this code that actually adds the portion after the decimal on to
numReadable
.*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
Arpikusz wrote:
numReadable = "," + numReadable;
What happens if the culture is one that uses . instead of , for the separator? Not only is this bad code, it's lazy bad code because it's not taking localisation into account. Also, as I look over the code, what happens if num is 3000.27? I can't see anywhere in this code that actually adds the portion after the decimal on to
numReadable
.*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
Its an abomination, for which the author should be sumarily executed. It takes a decimal, floors it, and outputs it with commas as the thousand saparator: http://rextester.com/CAYFY90912[^] It boils down to this one-liner:
var result = Math.Floor(num).ToString("#,#");
Cool site! And I agree!
Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions. Dave Barry Read more at [BrainyQuote](http://www.brainyquote.com/quotes/topics topic_technology.html#yAfSEbrfumitrteO.99)[^]
-
Can anyone tell me what this code does? :-D I found it in a project I inherited:
public static string ConvertToReadableNumber(decimal num)
{
bool isNegative = num<0;
num = Math.Floor(num);
string numTxt = Math.Abs(num).ToString();
string numReadable = "";
int counter =0;
for (int i = numTxt.Length-1; i >=0; i--)
{
if (counter > 0 && counter % 3 == 0)
numReadable = "," + numReadable;
numReadable = numTxt[i] + numReadable;
counter++;
}
if(isNegative)
numReadable = "-"+numReadable;
return numReadable;
}I'm guessing that a ToString() would have done the job. :)
-
Can anyone tell me what this code does? :-D I found it in a project I inherited:
public static string ConvertToReadableNumber(decimal num)
{
bool isNegative = num<0;
num = Math.Floor(num);
string numTxt = Math.Abs(num).ToString();
string numReadable = "";
int counter =0;
for (int i = numTxt.Length-1; i >=0; i--)
{
if (counter > 0 && counter % 3 == 0)
numReadable = "," + numReadable;
numReadable = numTxt[i] + numReadable;
counter++;
}
if(isNegative)
numReadable = "-"+numReadable;
return numReadable;
}I'm guessing that a ToString() would have done the job. :)
-
Its an abomination, for which the author should be sumarily executed. It takes a decimal, floors it, and outputs it with commas as the thousand saparator: http://rextester.com/CAYFY90912[^] It boils down to this one-liner:
var result = Math.Floor(num).ToString("#,#");
-
Can anyone tell me what this code does? :-D I found it in a project I inherited:
public static string ConvertToReadableNumber(decimal num)
{
bool isNegative = num<0;
num = Math.Floor(num);
string numTxt = Math.Abs(num).ToString();
string numReadable = "";
int counter =0;
for (int i = numTxt.Length-1; i >=0; i--)
{
if (counter > 0 && counter % 3 == 0)
numReadable = "," + numReadable;
numReadable = numTxt[i] + numReadable;
counter++;
}
if(isNegative)
numReadable = "-"+numReadable;
return numReadable;
}I'm guessing that a ToString() would have done the job. :)
if its C#, how about the standard .net formatting codes:
Math.Floor(num).ToString("N0")
-
And that's why it is a readable number. Use whole numbers to avoid confusions like '.' or ','. :)
"Fear no factor", Prime Numbers.
Agreed, clearly the assumption here is that a decimal number is not readable :)