• Status: Solved
• Priority: Medium
• Security: Public
• Views: 224

# format a string (larger number - filesize) as n kB

How can I format a string that holds a number like:

34789 to be displayed as 35 kB
or
34236 to be displayed as 34 kB
or
123612 to be displayed as 124 kB

Hope someone can help me!

/ joergen
0
jastroem
• 2
1 Solution

Commented:
Something like this should work:

<cfset kb = int(round(34789/1000))>
34789 = <cfoutput>#kb#</cfoutput> kB
<br>
<cfset kb = int(round(34236/1000))>
34236 = <cfoutput>#kb#</cfoutput> kB
<br>
<cfset kb = int(round(123612/1000))>
123612 = <cfoutput>#kb#</cfoutput> kB

Also, don't forget that

1024 bytes = 1 KB

so, the math is not necessarily this simple, but those are the numbers you asked for.
0

Author Commented:
ndintenfass

>Also, don't forget that
>1024 bytes = 1 KB

So then it should be?

<cfset kb = int(round(34789/1024))>
34789 = <cfoutput>#kb#</cfoutput> kB

/ joergen
0

Commented:
Here it is!

<CFIF SIZE lt 2000>                                                                                          #Trim(NumberFormat(SIZE,'____'))#&nbsp;bytes
<CFELSEIF SIZE gt 2000000>
#Trim(NumberFormat(SIZE/1024/1024,'__________.0'))#&nbsp;M
<CFELSE>
#Trim(NumberFormat(SIZE/1024,'____'))#&nbsp;K
</CFIF>

Size is the variable u want to change.

Jimmy
0

Author Commented:
jimmy282

right - that's exactly what I need!

thank you both

/joergen
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.