Classic ASP / Round() - Show Numbers 10,000+ as 10K - asp-classic

Like on Stack Overflow's reputation points, if a number is greater than ten thousand, they show it as 10.3K, to save space. Do I achieve this using the Round function or is better to use some kind of string manipulation?

I'd use FormatNumber() to shorten the number when necessary, here's some sample code to demonstrate:
<%
' Number scale I used:
' http://www.statman.info/conversions/number_scales.html
Function shorten(s)
Dim i, f
i = CDbl(s)
If (i > 1000000000000) Then
i = i / 1000000000000
f = "T"
ElseIf (i > 1000000000) Then
i = i / 1000000000
f = "G"
ElseIf (i > 1000000) Then
i = i / 1000000
f = "M"
ElseIf (i > 1000) Then
i = i / 1000
f = "K"
End If
shorten = FormatNumber(i, 2) & f
End Function
Response.Write shorten("1346578977987") & "<br>"
Response.Write shorten("1645112877") & "<br>"
Response.Write shorten("1313333") & "<br>"
Response.Write shorten("108977") & "<br>"
%>

Related

Change response to only respond one set of values

I am very novice at asp. Here is my code:
response.write("<script>")
dim counter: counter = 0
do until rs.EOF
for each x in rs.Fields
counter = counter + 1
response.write "var CC" & counter & "=" & x.value & ";"
next
rs.MoveNext
loop
response.write("</script>")
This currently returns both columns from my DB:
<script>var CC1=ALFKI;var CC2=13579;var CC3=ALFKI;var CC4=246;</script>
I only want the second column to return, which is the numbers (13579 & 246).
What should I change?
Not sure I should be answering this, but to loop and only write out on the even iteration use Mod() to test the remainder.
response.write("<script>")
dim counter: counter = 0
do until rs.EOF
for each x in rs.Fields
counter = counter + 1
'Use Mod() to check counter is even
If counter Mod 2 = 0 Then response.write "var CC" & counter & "=" & x.value & ";"
next
rs.MoveNext
loop
response.write("</script>")
Useful links
The Magical Mod Function
Ad an IF statement, based on your counter.
for each x in rs.Fields
counter = counter + 1
if(counter = 1) then
response.write "var CC" & counter & "=" & x.value & ";"
end if
next
Try this
response.write("<script>")
dim counter: counter = 0
do until rs.EOF
for each x in rs.Fields
counter = counter + 1
if IsNumeric(x.value) then
response.write "var CC" & counter & "=" & x.value & ";"
end if
next
rs.MoveNext
loop
response.write("</script>")
The trick is to check "If IsNumeric" then do what you want.

Resize multidimensional array and sort them by date

I think my code successfully creates the multi dimensional array because I get the right amount when I count it with UBound(DataArray).
But I get null value when I try to display one of the data as Response.Write DataArray(1,0).
Any help appreciated!
sDateArray = Split(DateArray, ",")
sVenueArray = Split(VenueArray, ",")
Dim DataArray()
For i = 0 to uBound(sDateArray)-1
ReDim DataArray(i, 1)
DataArray(i, 0) = sDateArray(i)
DataArray(i, 1) = sVenueArray(i)
Next
Response.Write UBound(DataArray) & "<br /><br />"
DataArray(1,0)
Response.Write DataArray(1,0)
Try Redim Preserve DataArray(i, 1) instead of ReDim DataArray(i, 1)
...or...
sDateArray = Split(DateArray, ",")
sVenueArray = Split(VenueArray, ",")
Dim DataArray(uBound(sDateArray)-1, 1)
For i = 0 to uBound(sDateArray)-1
DataArray(i, 0) = sDateArray(i)
DataArray(i, 1) = sVenueArray(i)
Next
Response.Write UBound(DataArray) & "<br /><br />"
' DataArray(1,0) ' <== commented out cos I think this might be an error - ?
Response.Write DataArray(1,0)
Ok I was bored so I wrote this.
May not be perfect - it's been a while since I used Classic ASP
Function SortByDate(a_input)
x = UBound(a_input, 1) - 1
if( x < 1 ) Then
Response.Write "<p>Invalid input array - first element is empty</p>"
Stop
End If
Dim a_output(x, 1)
Dim earliest_date
For j=0 To x
earliest_date = -1
For i=0 To UBound(a_input, 1) - 1
If a_input(0, i) <> "" Then
If earliest_date = -1 Then
earliest_date = i
Else
If CDate(a_input(0,i)) > CDate(a_input(0,earliest_date)) Then
earliest_date = i
End If
End If
End If
Next
a_output(0, i) = a_input(0, earliest_date)
a_output(1, i) = a_input(1, earliest_date)
a_input(0, earliest_date) = "" ' this one is done so skip next time '
Next
SortByDate = a_output
End Function

For...Next Loop Multiplication Table to Start on 0

I have my For...Next loop with a multiplication table working just fine, but I want the top left box to start at 0 and move on from there. Giving me some trouble.
dim mult
mult = "<table width = ""100%"" border= ""1"" >"
For row = 1 to 50
mult = mult & "<tr align = ""center"" >"
For col= 1 to 20
mult = mult & "<td>" & row * col & "</td>"
Next
mult = mult & "</tr>"
Next
mult = mult & "</table>"
response.write mult
This is what I have so far. Any suggestions?
Just change this piece of code
For row = 1 to 50
for this one
For row = 0 to 50
Just change this piece of code
For row = 1 to 50
for this one
For row = 1 to 51
I think this will help you.

How to format a datetime with minimal separators and timezone in VBScript?

I have the following code in C#:
DateTime dt = GetDateTime();
string formatted = dt.ToString("yyyyMMddTHHmmsszz");
which returns a date in the following format:
20100806T112917+01
I would like to be able to get the same results in VBScript (for a legacy ASP application). It is especially important that I get the UTC offset information, or have the time converted to UTC.
How do I do that?
For date formatting, I like using the .NET StringBuilder class from VBScript:
Option Explicit
Dim sb : Set sb = CreateObject("System.Text.StringBuilder")
sb.AppendFormat "{0:yyyyMMddTHHmmsszz}", Now()
Response.Write sb.ToString()
The above returns:
20100806T201139-07
This assumes that you have .NET installed on your web server.
Here's my own attempt. Better solutions will be graciously accepted!
Function ToDateTimeStringMinimalSeparators(dateTime)
' --------------------------------------------------------------------------
' F O R M A T T H E U T C O F F S E T
' --------------------------------------------------------------------------
Dim oShell, activeTimeBias
Set oShell = CreateObject("WScript.Shell")
activeTimeBias = oShell.RegRead("HKEY_LOCAL_MACHINE\System\" & _
"CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias")
Dim sign
sign = "-"
If activeTimeBias < 0 Then
sign = "+"
' Make it positive while we're doing calculations
activeTimeBias = activeTimeBias * -1
End If
Dim atbHours, atbMins
atbHours = Right("00" & Int(activeTimeBias / 60), 2)
atbMins = Right("00" & (activeTimeBias Mod 60), 2)
If atbMins = "00" Then
atbMins = ""
End If
Dim utcOffset
utcOffset = sign & atbHours & atbMins
' --------------------------------------------------------------------------
' F O R M A T T H E M A I N D A T E
' --------------------------------------------------------------------------
Dim dateBody
dateBody = Right("0000" & Year(dateTime), 4) & _
Right("00" & Month(dateTime), 2) & _
Right("00" & Day(dateTime), 2) & _
"T" & _
Right("00" & Hour(dateTime), 2) & _
Right("00" & Minute(dateTime), 2) & _
Right("00" & Second(dateTime), 2)
ToDateTimeStringMinimalSeparators = dateBody & utcOffset
End Function

asp pagination problem

Hi i have a problem with this asp pagination. it seems to be putting all the links in the one row, so i think it might have something to do with the check of the int i...
but im not that familar with asp. can anyone shed any light on this problem.
the folders contain pdfs for each day of the month, named A08P2.pdf A09P2.pdf etc...
Thanks
i = 1
Set fc = f.Files
Set ff = f.SubFolders
For Each f1 in fc
intPage = cint(mid(f1.name,2,2))
chrEdition = mid(f1.name,1,1)
if chrEdition = "A" then
if i = 1 then
Response.Write "<tr>"
end if
Response.Write "<td width='40' align='center'><a href=" & sUP & f1.name & " class='blue_11px'>" & intPage & "</a></td>"
if i = 10 then
Response.Write "</tr>"
i = 0
end if
end if
i = i + 1
Next
You should move the incrementing of the i (i=i+1) inside the if...end if, since if i is 9 and you encounter two chrEditions that are not 'A' then i will become 11 and will never match the closing condition i=10:
if chrEdition = "A" then
if i = 1 then
Response.Write "<tr>"
end if
Response.Write "<td width='40' align='center'><a href=" & sUP & f1.name & " class='blue_11px'>" & intPage & "</a></td>"
if i = 10 then
Response.Write "</tr>"
i = 0
end if
i = i + 1
end if

Resources