Does C# have a built-in way to format numbers like this?

  • Thread starter Thread starter kirk
  • Start date Start date
K

kirk

I guess the easiest way to describe what I need for output is to give
some examples. The biggest rule is the number cannot be larger than 3
digits after the formatting.

--examples--
before: 30, after: 30
before: 300, after: 300
before: 3000, after: 3K
before: 30000, after: 30K
before: 30500, after: 30.5K // K meaning Kilo
before: 300000000, after: 300M
before: 300500000, after: 301M
before: 300050000, after: 300M // M meaning Mega
before: 300000000000, after: 300T // T meaning Tera
....
....
....
 
I guess the easiest way to describe what I need for output is to give
some examples. The biggest rule is the number cannot be larger than 3
digits after the formatting.

No, there isn't anything built into the framework to do what you're
suggesting.

Jon
 

That's called engineering notation (a variant of scientific notation where
the exponent is a multiple of 3).
No, there isn't anything built into the framework to do what you're
suggesting.

I can't find anything either.
 
As Jon and Ben said, there is nothing built in. However, it isn't that
difficult to code.

public string ConvertToFriendlySize(UInt64 fileSize)
{
String[] suffixes = { "Bytes", "KB", "MB", "GB", "TB", "PB", "EB" };
int mult = 0;
double dFileSize = (double)fileSize;
while ((dFileSize >= 1024.0) && (mult < suffixes.Length - 1))
{
dFileSize /= 1024.0;
mult++;
}
return String.Format("{0} {1}", dFileSize.ToString("0.00"),
suffixes[mult]);
}
 
As Jon and Ben said, there is nothing built in. However, it isn't that
difficult to code.

public string ConvertToFriendlySize(UInt64 fileSize)
{
String[] suffixes = { "Bytes", "KB", "MB", "GB", "TB", "PB", "EB" };
int mult = 0;
double dFileSize = (double)fileSize;
while ((dFileSize >= 1024.0) && (mult < suffixes.Length - 1))
{
dFileSize /= 1024.0;
mult++;
}
return String.Format("{0} {1}", dFileSize.ToString("0.00"),
suffixes[mult]);
}




I guess the easiest way to describe what I need for output is to give
some examples. The biggest rule is the number cannot be larger than 3
digits after the formatting.
--examples--
before: 30, after: 30
before: 300, after: 300
before: 3000, after: 3K
before: 30000, after: 30K
before: 30500, after: 30.5K // K meaning Kilo
before: 300000000, after: 300M
before: 300500000, after: 301M
before: 300050000, after: 300M // M meaning Mega
before: 300000000000, after: 300T // T meaning Tera
...
...
...- Hide quoted text -

- Show quoted text -

Thanks Kelly. I'm looking for the most optimized solution and was a
little opposed to the while loop. Not sure if mine is any faster?
The method could be called every 1/10 of a second under extreme
conditions. Any suggestions on optimizing either solution or which is
faster?

static readonly String[] szSymbol = { "", "k", "M", "G", "T", "P",
"E", "Z", "Y", "X", "W", "V", "U"};
const String UNIT_OF_MEASURE = "bps";

static string FormatEngineeringNotation(decimal dcmNumber)
{
int intSIPrefix1000N = (Convert.ToString(dcmNumber).Length -
1) / 3;
double dblSIPrefixDecimal = Math.Pow(1000,
(double)intSIPrefix1000N);
double dblFormattedNumber = ((double)dcmNumber /
dblSIPrefixDecimal);

return String.Concat(dblFormattedNumber.ToString("#.#"),
szSymbol[intSIPrefix1000N], UNIT_OF_MEASURE);
}
 
kirk said:
Thanks Kelly. I'm looking for the most optimized solution and was a
little opposed to the while loop. Not sure if mine is any faster?
The method could be called every 1/10 of a second under extreme
conditions. Any suggestions on optimizing either solution or which is
faster?

10 times a second isn't going to register above noise. Use the simplest
possible solution until you've got a good (measured) reason to believe
it's too slow.

On my 2-year-old laptop, calling the method you object to 10 *million*
times takes 14 seconds. In other words, each call takes 14
*microseconds* on average. If you're calling it 10 times per second,
that will cost you 140 microseconds. Are you still worried?

Running your method for the same number of iterations took just over 15
seconds, btw. Even though the while loop may have bothered you
instinctively, inspection shows it's only going to run at most 7 times
(or 6, or 8 - I haven't checked for off-by-one errors) so it's hardly
going to be a killer.
 
10 times a second isn't going to register above noise. Use the simplest
possible solution until you've got a good (measured) reason to believe
it's too slow.

On my 2-year-old laptop, calling the method you object to 10 *million*
times takes 14 seconds. In other words, each call takes 14
*microseconds* on average. If you're calling it 10 times per second,
that will cost you 140 microseconds. Are you still worried?

Running your method for the same number of iterations took just over 15
seconds, btw. Even though the while loop may have bothered you
instinctively, inspection shows it's only going to run at most 7 times
(or 6, or 8 - I haven't checked for off-by-one errors) so it's hardly
going to be a killer.

Thanks for the input Jon.

I agree, we're talking microseconds here, which is pretty negligible.
I mimicked your test on my end, running each method in a loop 10
million times, and when passing a single digit number, there is a
difference of 8 seconds between both methods and when passing a 19
digit number, there is a difference of 20 seconds, my method being the
slower of the two in all instances of course. This difference, while
small at the microsecond level, had me curious. I found that pulling
the "Convert.ToString" statement at the beginning of my method and
substituting it for the implementation below made my method perform
faster than Kelly's. Faster by 3 seconds when passing a single digit
and faster by 1 second when passing 19 digits.

Code I used in place of the "Convert.ToString" statement at the
beginning of my method....
int intSIPrefix1000N = (int)(Math.Log10(dblNumber) / 3);

The Math class by the way is extremely fast, using hardcoded numbers
in place of the above for example, give the same speed even when
looping the code 10 million times. Pretty impressive.
 
Back
Top