Thursday, September 09, 2004

formatting number in VBScript

My current work is to display data statistics or data reporting, so my work always shows tables of numbers and charts. The numbers involved here could be millions, billions or even trillions. For a user friendly looks (display), I have to convert from 123456789.01 into 123,456,789.01 (using commas). I'm using ASP with VBScript for my current work and here is my source code for converting numbers into numbers with commas.
function commanum (valori)
   dim retval0, val0, val0temp
   if inStr(valori,".")>0 then
      val0=Left(valori, inStr(valori,".")-1)
      val0temp = _
         Right(valori, len(valori)-inStr(valori,".")+1)
   else	
      val0 = valori
      val0temp=""
   end if
   if val0<>"-" and val0<>"" and isNumeric(val0) then
      if val0 >= 1000 then
         dim n, valtemp, i, deltemp, valx
         valtemp = val0
         n = 0
         while valtemp>=1000
            valtemp = int(valtemp/1000)
            n = n + 1
         wend
         deltemp = 1000
         for i=1 to n
            retval0 = commanum1(val0, deltemp, ",") _
               & retval0
            val0 = int( val0 / 1000)
            if (val0>0) then
               valx = val0
            end if
         next
         retval0 = valx & retval0
      else 
         retval0 = val0
      end if
   else
      retval0 = val0
   end if
   commanum = retval0&val0temp
end function

function commanum1 (val1, delim, comma)
   if val1 >= delim then
      commanum1 = comma & right(val1,len(delim)-1)
   else 
      commanum1 = val1
   end if
end function
To use the function, first you have to convert the variable into number (using cDbl or cLng) if the variable is not a number. For example:
Response.write commanum(cDbl("50549455490260"))
And the result will be: 50,549,455,490,260 Okay, formating numbers using commas is done. How about rounding off a number with 2 decimal behind a dot? You can say that it's so easy. Just use round function that already defined in VBScript. But I have my own problem. Using VBScript's round function, if the last decimal (behind the dot) is "0", then that "0" is removed. For example, round(1234.50123,2) will results 1234.5 Where is my last zero here? Why should I concern about this zero? Well, as I've told you, my works involved table of numbers, if the zero is not shown, numbers in a column which are justified to the right will have a bumpy-looks. For example:
70.71
 13.5
12.34
 3.45
Looks bad, huh? This is my solution for the zero problem:
function round_into(val1, val2, val3)
   dim tempround
   tempround = round(val1,val2) & ""
   if val3>0 then
      if inStr(tempround,".")=0 then
         tempround=tempround & "."
      end if
      do while (len(tempround)-inStr(tempround,"."))<val3
         tempround = tempround & "0"
      loop
   end if
   round_into = tempround
end function      

function round_into_kus(val1, val2)
   round_into_kus = round_into(val1, val2, val2)
end function
Gee, don't know if this is the best solution, but it works. Okey then, jyaaa ne.

2 Comments:

At 9:50 PM, Anonymous Anonymous said...

Hi kus, your blog is excellent. As I was surfing around today looking for detailed info on residential mortgage I somehow ended up on your page. As your formatting number in VBScript is not exactly related to my search, I am certainly glad I stopped by. Oh well, back to surfing and I am sure I will find what I am looking for, and should you ever need information about residential mortgage, then stop by for a look. Thanks for the post.

 
At 3:56 AM, Anonymous Anonymous said...

Hi kus...Wow! While I was searching for info on mortgage life insurance I somehow found your page. Obviously I ended up a little off base, but I am certainly glad I stopped by for a read. While I am here, I just wanted to drop a quick note to comment your blog...now to move on and continue my search for mortgage life insurance. Should you ever need it, there's lots of information on this site about mortgage life insurance.

 

Post a Comment

<< Home