View Full Version : Converting bytes to kilobytes
mattboy_slim
05-05-2003, 01:33 AM
I have a field in my table that has all data entered in as bytes. I'd like the output to show kilobytes instead:
Currently have:
<td width="50" bgcolor="#CCE6FF"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<%=(rs_reporting.Fields.Item("MsgSize").Value)%> bytes</font></td>
How can I convert this data to show the latter?
Thanks,
Matt
mattboy_slim
05-05-2003, 02:29 AM
I found this function that I think will work, but I'm not sure how to call it from my current code:
<%
Function SetBytes(Bytes)
If Bytes >= 1073741824 Then
SetBytes = Format(Bytes / 1024 / 1024 / 1024, "#0.00") _
& " GB"
ElseIf Bytes >= 1048576 Then
SetBytes = Format(Bytes / 1024 / 1024, "#0.00") & " MB"
ElseIf Bytes >= 1024 Then
SetBytes = Format(Bytes / 1024, "#0.00") & " KB"
ElseIf Bytes < 1024 Then
SetBytes = Fix(Bytes) & " Bytes"
End If
End Function
%>
Luke Redpath
05-05-2003, 04:00 AM
That function looks like it will work although I've not tested it.
To call it, just do this:
<%
Dim strBytes
strBytes = rs_reporting.Fields.Item("MsgSize").Value ' NOT REQUIRED BUT MAKES THINGS EASIER
%>
<td width="50" bgcolor="#CCE6FF">
<font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<%=(SetBytes(strBytes))%>
</font>
</td>
oracleguy
05-05-2003, 06:09 AM
I don't think there is a format method in ASP. There is in VB but not is ASP 3.
mattboy_slim
05-05-2003, 03:02 PM
You seem to be right OracleGuy.
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'Format'
/reporting/default2.asp, line 29
I cut out what I didn't need, and was left with the following. Know how to fix it?
Function SetBytes(Bytes)
If Bytes >= 1024 Then
SetBytes = Format(Bytes / 1024, "#0.00") & " KB"
ElseIf Bytes < 1024 Then
SetBytes = Fix(Bytes) & " Bytes"
End If
End Function
Cant you just use the round() or FormatNumber() function ?
Round is the easiest. Just
variable = Round(number, 2) will round it to two decimals.
If Bytes >= 1024 Then
SetBytes = Round(Bytes / 1024, 2) & " KB"
ElseIf Bytes < 1024 Then
SetBytes = Fix(Bytes) & " Bytes"
End If
(fix could probably also be dropped?)
More info on FormatNumber from the helpfile
--------------------------------------------------
Description
Returns an expression formatted as a number.
Syntax
FormatNumber(Expression [,NumDigitsAfterDecimal [,IncludeLeadingDigit [,UseParensForNegativeNumbers [,GroupDigits]]]])
The FormatNumber function syntax has these parts:
Part Description
Expression Required. Expression to be formatted.
NumDigitsAfterDecimal Optional. Numeric value indicating how many places to the right of the decimal are displayed. Default value is -1, which indicates that the computer's regional settings are used.
IncludeLeadingDigit Optional. Tristate constant that indicates whether or not a leading zero is displayed for fractional values. See Settings section for values.
UseParensForNegativeNumbers Optional. Tristate constant that indicates whether or not to place negative values within parentheses. See Settings section for values.
GroupDigits Optional. Tristate constant that indicates whether or not numbers are grouped using the group delimiter specified in the control panel. See Settings section for values.
Settings
The IncludeLeadingDigit, UseParensForNegativeNumbers, and GroupDigits arguments have the following settings:
Constant Value Description
TristateTrue -1 True
TristateFalse _0 False
TristateUseDefault -2 Use the setting from the computer's regional settings.
Remarks
When one or more of the optional arguments are omitted, the values for omitted arguments are provided by the computer's regional settings.
Note__All settings information comes from the Regional Settings Number tab.
The following example uses the FormatNumber function to format a number to have four decimal places:
Function FormatNumberDemo
Dim MyAngle, MySecant, MyNumber
MyAngle = 1.3 ' Define angle in radians.
MySecant = 1 / Cos(MyAngle) ' Calculate secant.
FormatNumberDemo = FormatNumber(MySecant,4) ' Format MySecant to four decimal places.
End Function
mattboy_slim
05-05-2003, 03:26 PM
Okay, I scratched that idea. I found a different one instead. I'm now getting the page to render, but every value is coming out the same:
See code:
http://www.rdi1.com/report.txt
All values are coming out as:
133.86 kb
Thanks
Luke Redpath
05-05-2003, 03:32 PM
Originally posted by mattboy_slim
Okay, I scratched that idea. I found a different one instead. I'm now getting the page to render, but every value is coming out the same:
See code:
http://www.rdi1.com/report.txt
All values are coming out as:
133.86 kb
Thanks
Hi.
Nice and easy one this. Just move:
strBytes = rs_reporting.Fields.Item("MsgSize").Value
inside your recordset loop. Keep
Dim strBytes
OUTSIDE the loop, or you will get a variable has been redefined error.
Should have made this point in my last post, sorry.
mattboy_slim
05-05-2003, 03:51 PM
Thanks raf and Luke, she works now.
All data is converted to kb when displayed now.
I've got about 100 more questions to make this project complete though.
Luke Redpath
05-05-2003, 05:04 PM
Originally posted by mattboy_slim
Thanks raf and Luke, she works now.
All data is converted to kb when displayed now.
I've got about 100 more questions to make this project complete though.
I don't browse these forums often, so if you have any questions, feel free to e-mail me: cont\act@lukeredpath.co.uk - remove the \ (spam protection).
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.