|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Does C# have a built-in way to format numbers like this?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 .... .... .... On Nov 28, 8:19 am, kirk <kir***@gmail.com> wrote:
> I guess the easiest way to describe what I need for output is to give No, there isn't anything built into the framework to do what you're> some examples. The biggest rule is the number cannot be larger than 3 > digits after the formatting. suggesting. Jon "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message That's called engineering notation (a variant of scientific notation where news:b7492d0b-996b-4af3-b53c-b6b96141a87c@b40g2000prf.googlegroups.com... > On Nov 28, 8:19 am, kirk <kir***@gmail.com> wrote: >> 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. the exponent is a multiple of 3). > I can't find anything either.> No, there isn't anything built into the framework to do what you're > suggesting. Show quote > > Jon 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]); } Show quote "kirk" <kir***@gmail.com> wrote in message news:a822cda0-f2f4-43de-9319-6803c6e6922b@s12g2000prg.googlegroups.com... >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 > ... > ... > ...
Show quote
On Nov 28, 3:12 pm, "Kelly Herald" <k...@nospam.no> wrote: Thanks Kelly. I'm looking for the most optimized solution and was a> 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]); > } > > "kirk" <kir***@gmail.com> wrote in message > > news:a822cda0-f2f4-43de-9319-6803c6e6922b@s12g2000prg.googlegroups.com... > > > > >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 - 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 <kir***@gmail.com> wrote:
> Thanks Kelly. I'm looking for the most optimized solution and was a 10 times a second isn't going to register above noise. Use the simplest > 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? 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. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk |
|||||||||||||||||||||||