Click here to Skip to main content
15,890,947 members
Articles / Programming Languages / C#
Tip/Trick

International System of Units Notation

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
26 Jan 2015CPOL2 min read 12.9K   101   7   4
A simple class for abbreviating integers with SI suffixes common in computer use.

Introduction

In this simple tip I will share with you some code and thoughts about the International System of Units Notation. The SI Class is a simple collection of functions and a demonstration / valdation program for using concepts from the International System of Units Notation for abbreviating integers with suffixes common in computer use. This is similar to exponential representation but a bit more compact and elegant. I often use these functions in multiple applications. 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 have included a demonstration / valdation SI_Validate program to illustrate the possibilities and verify by visual inspection that it does what I want. I have not yet perfected the class to quite the level I want. The original implementation used only integers but was modified to add a decimal place to improve significance where there was room 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 using some of the International System of Units suffixes common in computer use. This approximates and abbreviates positive integer numbers. I limit ToString to 4 characters or less with 2 or 3 digits precision. I also parse positive 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.

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.

An extended version of my ongoing thoughts can be found online.

I look forward to your observations and comments.

Partial Sample Demonstration Output

                   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...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer IAUA End Time Ministry
United States United States
I am a retired Software Systems Design Engineer experienced with IEEE standards and the entire IEEE software development life cycle. Concept Exploration, Requirements, Design, Implementation, Test, Installation and Checkout, Operation and Maintenance. I enjoy working with people and solving customer problems.

I am currently a writer for my personal ministry: IAUA End Time Ministry

Comments and Discussions

 
QuestionUpper-cased scale prefixes? Pin
John Brett26-Jan-15 5:24
John Brett26-Jan-15 5:24 
GeneralRe: Upper-cased scale prefixes? Pin
PIEBALDconsult26-Jan-15 5:28
mvePIEBALDconsult26-Jan-15 5:28 
GeneralRe: Upper-cased scale prefixes? Pin
John Brett26-Jan-15 5:33
John Brett26-Jan-15 5:33 
AnswerRe: Upper-cased scale prefixes? Pin
Frank T. Clark26-Jan-15 7:45
professionalFrank T. Clark26-Jan-15 7:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.