SI Class

International System of Units Notation

C# .Net 3.5 (Windows 7)
Windows Forms Application
with source code

Version 1.2 Released 2015-03-12

/Home /Professional /Papers /SI

Description Revised 2015-03-17

In this description you may find items marked in GREEN that are recent changes. You may also find items marked in OLIVE that are not in the current version but are planned for a later version. You may also find items marked in RED that only apply to the current version and will be changed in a later version.

Download Program and source

SI.zip


Contents

Introduction

The SI Class is a simple collection of functions and includes a demonstration / valdation program for using the International System of Units Notation (Wikipedia) for abbreviating unsigned integers with suffixes common in computer use. This is similar to exponential representation but a bit more commonly used, compact, and elegant. I often use these functions in multiple applications where I don't want to output an enormous multi-digit number that is difficult to quickly grasp the magnitude, I don't care about the insignificant digits, and I prefer a more compact display

I was a bit surprised that I did not find anything already available to do this in the .Net library nor could I find any example code provided on the Internet. It is a simple thing but useful to have. I hope you find the idea useful to consider and save yourself a bit of time with my code.

I am using a strictly limited set of number suffixes suitable to the range of a 64 bit unsigned integer. The suffix is derived from the SI unit prefixes, that are appropriate to typical computer use for unsigned integers. The SI description refers to these in the context of unit prefixes. The class is using the prefix as a suffix to a number without reference to any units. It is possible to add a unit to the provided number when it is output making the number suffix a unit prefix.

I have included a demonstration / valdation SI_Validate program to illustrate the possibilities and verify by visual inspection that it does what I want. Proper rounding was a special concern.

I have not yet perfected the class to quite the level I want. The original implementation used only unsigned integers but was modified to add a decimal place to improve significance where there was room in my strict four character limit design for a decimal place. I used a shortcut of double precision variables that is not efficient or elegant but it works and I haven't felt motivated to perfect it yet.

My goal was to have simple formatting and parsing of natural numbers (but including zero) using some of the International System of Units suffixes common in computer use. This approximates and abbreviates unsigned integer numbers. I limit ToString to 4 characters or less with 2, 3, or 4 digits precision. I also parse unsigned integer numbers from an SI string, which might be abbreviated.

In line with the SI definitions I use powers of 1000 not 2^10, which you often find with computer uses. Unlike the specification the suffix I use is always capitalized, and is not separated by a space. The specifications says "k" for kilo but I use "K", which is a common use. I am just being stingy leaving out the space separator.

Using the Code

I am providing the complete VS2013 C# project zip file with the executable, documentation, and the complete source code for the SI Class and the SI_Validate program for those who use and enjoy software code.

The SI_Validate program is a console application compiled in VS2013 Express with the .NET framework version 3.5, which is included automatically with the Windows 7 OS. The program requires nothing but the supplied source and standard Windows functionality with VS2013 Express. There are no third-party libraries used. There is no separate DLL required or used. The program has only been well used and thoroughly tested with the Windows 7 OS.

Example Output

The following tables illustrate the complete output of the validation program. I test the full range of some sample edge numbers.

A single digit shifted through all the appropriate factors of ten:

                   0 =    0 =                    0
                   1 =    1 =                    1
                  10 =   10 =                   10
                 100 =  100 =                  100
                1000 = 1000 =                 1000
               10000 =  10K =                10000
              100000 = 100K =               100000
             1000000 = 1.0M =              1000000
            10000000 =  10M =             10000000
           100000000 = 100M =            100000000
          1000000000 = 1.0G =           1000000000
         10000000000 =  10G =          10000000000
        100000000000 = 100G =         100000000000
       1000000000000 = 1.0T =        1000000000000
      10000000000000 =  10T =       10000000000000
     100000000000000 = 100T =      100000000000000
    1000000000000000 = 1.0P =     1000000000000000
   10000000000000000 =  10P =    10000000000000000
  100000000000000000 = 100P =   100000000000000000
 1000000000000000000 = 1.0E =  1000000000000000000
10000000000000000000 =  10E = 10000000000000000000

Rounding up where appropriate:

                   1 =    1 =                    1
                  14 =   14 =                   14
                 149 =  149 =                  149
                1490 = 1490 =                 1490
               14900 =  15K =                15000
              149000 = 149K =               149000
             1490000 = 1.5M =              1500000
            14900000 =  15M =             15000000
           149000000 = 149M =            149000000
          1490000000 = 1.5G =           1500000000
         14900000000 =  15G =          15000000000
        149000000000 = 149G =         149000000000
       1490000000000 = 1.5T =        1500000000000
      14900000000000 =  15T =       15000000000000
     149000000000000 = 149T =      149000000000000
    1490000000000000 = 1.5P =     1500000000000000
   14900000000000000 =  15P =    15000000000000000
  149000000000000000 = 149P =   149000000000000000
 1490000000000000000 = 1.5E =  1500000000000000000
14900000000000000000 =  15E = 15000000000000000000

Rounding up where appropriate except for the special case of 4 significant digits:

                   1 =    1 =                    1
                  14 =   14 =                   14
                 149 =  149 =                  149
                1499 = 1499 =                 1499
               14990 =  15K =                15000
              149900 = 150K =               150000
             1499000 = 1.5M =              1500000
            14990000 =  15M =             15000000
           149900000 = 150M =            150000000
          1499000000 = 1.5G =           1500000000
         14990000000 =  15G =          15000000000
        149900000000 = 150G =         150000000000
       1499000000000 = 1.5T =        1500000000000
      14990000000000 =  15T =       15000000000000
     149900000000000 = 150T =      150000000000000
    1499000000000000 = 1.5P =     1500000000000000
   14990000000000000 =  15P =    15000000000000000
  149900000000000000 = 150P =   150000000000000000
 1499000000000000000 = 1.5E =  1500000000000000000
14990000000000000000 =  15E = 15000000000000000000

Not rounding up where appropriate:

                   1 =    1 =                    1
                  15 =   15 =                   15
                 150 =  150 =                  150
                1500 = 1500 =                 1500
               15000 =  15K =                15000
              150000 = 150K =               150000
             1500000 = 1.5M =              1500000
            15000000 =  15M =             15000000
           150000000 = 150M =            150000000
          1500000000 = 1.5G =           1500000000
         15000000000 =  15G =          15000000000
        150000000000 = 150G =         150000000000
       1500000000000 = 1.5T =        1500000000000
      15000000000000 =  15T =       15000000000000
     150000000000000 = 150T =      150000000000000
    1500000000000000 = 1.5P =     1500000000000000
   15000000000000000 =  15P =    15000000000000000
  150000000000000000 = 150P =   150000000000000000
 1500000000000000000 = 1.5E =  1500000000000000000
15000000000000000000 =  15E = 15000000000000000000

More rounding cases and the maximum values:

                   1 =    1 =                    1
                  15 =   15 =                   15
                 155 =  155 =                  155
                1550 = 1550 =                 1550
               15500 =  16K =                16000
              155000 = 155K =               155000
             1550000 = 1.6M =              1600000
            15500000 =  16M =             16000000
           155000000 = 155M =            155000000
          1550000000 = 1.6G =           1600000000
         15500000000 =  16G =          16000000000
        155000000000 = 155G =         155000000000
       1550000000000 = 1.6T =        1600000000000
      15500000000000 =  16T =       16000000000000
     155000000000000 = 155T =      155000000000000
    1550000000000000 = 1.6P =     1600000000000000
   15500000000000000 =  16P =    16000000000000000
  155000000000000000 = 155P =   155000000000000000
 1550000000000000000 = 1.6E =  1600000000000000000
15500000000000000000 =  16E = 16000000000000000000
        Int64.MaxValue
 9223372036854775807 = 9.2E =  9200000000000000000
        UInt64.MaxValue
18446744073709551615 =  18E = 18000000000000000000

Type the ENTER key when ready...

Release Notes

A history of changes to the SI program.
  • Version 2.0 Not Yet Released
    • Use a more elegant unsigned integer solution instead of resorting to double precision to handle decimal places.
  • Version 1.2 Released 2015-01-25
    • Polished for public release.
  • Version 1.1
    • Add a decimal place to improve significance where there is space within the 4 character limit.
  • Version 1.0
    • Initial Release.

Known errors to be fixed:
  • There may be other problems or errors that I don't have a clear understanding or description of yet.

Future changes being considered:
  • Use a more elegant unsigned integer solution instead of resorting to double precision to handle decimal places.
  • It has been suggested to extend the concept to double precision and long bit numbers but I almost never use these and I suspect that others rarely do so either.
  • There may be other useful additional features I have not thought of yet.