Link to home
Start Free TrialLog in
Avatar of jastroem
jastroem

asked on

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
Avatar of ndintenfass
ndintenfass

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.
Avatar of jastroem

ASKER

ndintenfass

Thank you for your answer!


>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
ASKER CERTIFIED SOLUTION
Avatar of jimmy282
jimmy282
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
jimmy282

right - that's exactly what I need!

thank you both

/joergen