Kalkulasi BMI pada Fungsi calculateBMI()
This commit is contained in:
parent
c69d20c1b7
commit
eb0720c592
@ -63,7 +63,9 @@ import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.example.bmicalculator.ui.theme.BMICalculatorTheme
|
||||
import java.text.DecimalFormat
|
||||
import java.text.NumberFormat
|
||||
import kotlin.math.pow
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
@ -86,12 +88,12 @@ class MainActivity : ComponentActivity() {
|
||||
fun BMICalculatorLayout() {
|
||||
var heightInput by remember { mutableStateOf("") }
|
||||
var weightInput by remember { mutableStateOf("") }
|
||||
var unitUSC by remember { mutableStateOf(false) }
|
||||
var unitUSC by remember { mutableStateOf(false) }
|
||||
|
||||
val BmiHeight = heightInput.toDoubleOrNull() ?: 0.0
|
||||
val BmiWeight = weightInput.toDoubleOrNull() ?: 0.0
|
||||
val bmi = calculateBMI(BmiHeight, BmiWeight, unitUSC)
|
||||
val category = calculateBMICategory(BmiHeight, BmiWeight, unitUSC)
|
||||
val BmiHeight = heightInput.toDoubleOrNull() ?: 0.0
|
||||
val BmiWeight = weightInput.toDoubleOrNull() ?: 0.0
|
||||
val bmi = calculateBMI(BmiHeight, BmiWeight, unitUSC)
|
||||
val category = calculateBMICategory(BmiHeight, BmiWeight, unitUSC)
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
@ -192,15 +194,28 @@ fun UnitUSCFormulaRow(
|
||||
|
||||
/**
|
||||
* Calculates the BMI
|
||||
* dengan Rumus SI Metrics Unit (Default)
|
||||
* dan
|
||||
* dengan Rumus USC Units
|
||||
*
|
||||
* Catatan: tambahkan unit test untuk kalkulasi BMI ini
|
||||
*/
|
||||
private fun calculateBMI(BmiHeight: Double, BmiWeight: Double = 15.0, unitUSC: Boolean): String {
|
||||
var bmi = BmiWeight / 100 * BmiHeight
|
||||
if (unitUSC) {
|
||||
bmi = kotlin.math.ceil(bmi)
|
||||
private fun calculateBMI(BmiHeight: Double, BmiWeight: Double, unitUSC: Boolean): String {
|
||||
if (BmiHeight <= 0 || BmiWeight <= 0){
|
||||
return "0"
|
||||
}
|
||||
return NumberFormat.getNumberInstance().format(bmi)
|
||||
|
||||
val heightInMeter = BmiHeight/100 // konversi centimeter ke meter
|
||||
val heightInInch = BmiHeight * 0.3937 // konversi centimeter ke inci
|
||||
val weightInPound = BmiWeight * 2.2046 // konversi berat badan ke pound (lbs)
|
||||
|
||||
var bmi = BmiWeight / heightInMeter.pow(2)
|
||||
if (unitUSC) {
|
||||
bmi = 703 * (weightInPound / heightInInch.pow(2))
|
||||
}
|
||||
|
||||
val df = DecimalFormat("#.#")
|
||||
return df.format(bmi)
|
||||
}
|
||||
/**
|
||||
* Calculates the BMI Category
|
||||
@ -221,4 +236,4 @@ fun TipTimeLayoutPreview() {
|
||||
BMICalculatorTheme {
|
||||
BMICalculatorLayout()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user