| View previous topic :: View next topic |
| Author |
Message |
aRTiST Voice
Joined: 17 Apr 2009 Posts: 4
|
Posted: Sat Apr 18, 2009 1:58 pm Post subject: Problem with formating units |
|
|
| Code: | proc format_1024_units {value} {
set len [string length $value]
if {$value < 1024} {
return [format "%s B" $value]
} else {
set unit [expr {($len - 1) / 3}]
return [format "%.1f %s" [expr {$value / pow(1024,$unit)}] [lindex [list B KB MB GB TB PB EB ZB YB] $unit]]
}
} |
But the result is then often 0.0TB (value is really 35.2GB unformated).
Any hints where im doing wrong ?
TIA arti |
|
| Back to top |
|
 |
speechles Revered One

Joined: 26 Aug 2006 Posts: 1398 Location: emerald triangle, california (coastal redwoods)
|
Posted: Sat Apr 18, 2009 11:22 pm Post subject: |
|
|
The problem here is your method of attaining size doesn't match throughout your procedure. You initially set "len" to the [string length $value]. Then you set "unit" to to "($len-1) / 3". This where you make your mistake. This assumes 1,000 for our divisor. Yet below that you make use of the correct 1,024 to raise powers which throws off the entire equation. Using the method below should solve your problem and its more compact.
| Code: | proc format_1024_units {value} {
set test $value ; set unit 0
while {[set test [expr {$test / 1024}]] > 0} {
incr unit
}
return [format "%.1f %s" [expr {$value / pow(1024,$unit)}] [lindex [list B KB MB GB TB PB EB ZB YB] $unit]]
} |
_________________ speechles' eggdrop tcl archive |
|
| Back to top |
|
 |
aRTiST Voice
Joined: 17 Apr 2009 Posts: 4
|
Posted: Sun Apr 19, 2009 5:25 am Post subject: hm |
|
|
I tried with ur version, but now ill get an error :
| Code: | | floating-point value too large to represent |
:/ |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sun Apr 19, 2009 9:28 am Post subject: |
|
|
Could you verify that $value is indeed an integer and not a float?
The code posted by speechles relies on integer division, and will fail if either the nominator or the denominator is a float. I'd also recommend adding a constraint to the value of unit, as to prevent it from going out of bounds for the "suffix-list".
You might be able to get away with forcing $test to an integer in the beginning. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
|