Math brain failing

Hi Youngsters (or oldsters still holding onto their little gray cells) -

I’m working with a percentages model and I can’t tell if it’s my mind or a Xojo thing.

I’m trying to get the percentage of a disk used and use this formula:

thePercentage = (theFullDiskSizeInBytes - (theFullDiskSizeInBytes - theUsedDiskSpaceInBytes)) / 100

All three properties are Doubles.

But, I’m getting strange values as the result and feel that rounding the resulting thePercentage to 2 decimals would sort things. Is my only option the multiply thePercentage by 100, Round that, and then divide that by 100?

thePercentage = Round(thePercentage * 100) / 100

Thanks for any suggestions or agreements with my insanity.
A completely befuddled old guy

This

is the same as:

(0 + theUsedDiskSpaceInBytes) / 100

Percentage = used/full * 100

so

thePercentage = theUsedDiskSpaceInBytes / theFullDiskSizeInBytes * 100

should do it, unless the size in bytes is crazy big
You can format to 2dp if you need to for display purposes.

The first part of the formula can be evaluated like this:

theFullDiskSizeInBytes - (theFullDiskSizeInBytes - theUsedDiskSpaceInBytes)
theFullDiskSizeInBytes - theFullDiskSizeInBytes + theUsedDiskSpaceInBytes
theUsedDiskSpaceInBytes

So your formula is actually:

thePercentage = theUsedDiskSpaceInBytes / 100

That’s taking a double and dividing by an integer, so try this instead:

thePercentage = theUsedDiskSpaceInBytes / 100.0

BUT, I think what you really want is…

thePercentage = ( theUsedDiskSpaceInBytes / theFullDiskSizeInBytes ) * 100.0

I totally know that, and I’m totally embarrassed that I was so busy worrying about the trig to create the pie chart that I fumbled the simple math …

:confounded:

Thanks to each of you.

2 Likes

Never been there, nosiree…

2 Likes