B4J Question Calculate Percentage %

G-ShadoW

Active Member
Licensed User
Longtime User
Hello,

I need to calculate percentage from number (+/- 1.5%) and block command to execute if that number is not higher or lower then 1.33%

Example:

So if 2.24568 <> 1.33% do nothing

Regards,
Tom
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hello,

I need to calculate percentage from number (+/- 1.5%) and block command to execute if that number is not higher or lower then 1.33%

Example:

So if 2.24568 <> 1.33% do nothing

Regards,
Tom
PercenageofNumber = number x 0.015
 
Upvote 0

emexes

Expert
Licensed User
I need to calculate percentage from number (+/- 1.5%) and block command to execute if that number is not higher or lower then 1.33%

Example:

So if 2.24568 <> 1.33% do nothing
B4X:
Dim ThermostatDegrees As Float = 1.5

Dim MaxErrorPercent As Float = 1.33
Dim MaxErrorDegrees As Float = Abs(ThermostatDegrees * MaxErrorPercent / 100)

Dim CurrentTemperatureDegrees As Float = 2.24568

If Abs(CurrentTemperatureDegrees - ThermostatDegrees) < MaxErrorDegrees Then
    Log("within bounds")    'temperature is within 1.33% of thermostat setting
    Log("(ie between " & (ThermostatDegrees - MaxErrorDegrees) & " and " & (ThermostatDegrees + MaxErrorDegrees) & " degrees)")
Else
    Log("sound the klaxon")
End If

although in many situations you'd be better testing separately for too-low and too-high because usually the responses are different, eg if monitoring refrigerator temperature then:
- if it goes too high, you'd prompt the operator to check that the door is closed and power is available and connected and switched on
- if it goes too low, you'd prompt the operator to check if the compressor motor is running continuously
 
Last edited:
Upvote 0
Top