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.tooling.preview.Preview
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import com.example.bmicalculator.ui.theme.BMICalculatorTheme
|
import com.example.bmicalculator.ui.theme.BMICalculatorTheme
|
||||||
|
import java.text.DecimalFormat
|
||||||
import java.text.NumberFormat
|
import java.text.NumberFormat
|
||||||
|
import kotlin.math.pow
|
||||||
|
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
@ -86,12 +88,12 @@ class MainActivity : ComponentActivity() {
|
|||||||
fun BMICalculatorLayout() {
|
fun BMICalculatorLayout() {
|
||||||
var heightInput by remember { mutableStateOf("") }
|
var heightInput by remember { mutableStateOf("") }
|
||||||
var weightInput 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 BmiHeight = heightInput.toDoubleOrNull() ?: 0.0
|
||||||
val BmiWeight = weightInput.toDoubleOrNull() ?: 0.0
|
val BmiWeight = weightInput.toDoubleOrNull() ?: 0.0
|
||||||
val bmi = calculateBMI(BmiHeight, BmiWeight, unitUSC)
|
val bmi = calculateBMI(BmiHeight, BmiWeight, unitUSC)
|
||||||
val category = calculateBMICategory(BmiHeight, BmiWeight, unitUSC)
|
val category = calculateBMICategory(BmiHeight, BmiWeight, unitUSC)
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
@ -192,15 +194,28 @@ fun UnitUSCFormulaRow(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates the BMI
|
* Calculates the BMI
|
||||||
|
* dengan Rumus SI Metrics Unit (Default)
|
||||||
|
* dan
|
||||||
|
* dengan Rumus USC Units
|
||||||
*
|
*
|
||||||
* Catatan: tambahkan unit test untuk kalkulasi BMI ini
|
* Catatan: tambahkan unit test untuk kalkulasi BMI ini
|
||||||
*/
|
*/
|
||||||
private fun calculateBMI(BmiHeight: Double, BmiWeight: Double = 15.0, unitUSC: Boolean): String {
|
private fun calculateBMI(BmiHeight: Double, BmiWeight: Double, unitUSC: Boolean): String {
|
||||||
var bmi = BmiWeight / 100 * BmiHeight
|
if (BmiHeight <= 0 || BmiWeight <= 0){
|
||||||
if (unitUSC) {
|
return "0"
|
||||||
bmi = kotlin.math.ceil(bmi)
|
|
||||||
}
|
}
|
||||||
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
|
* Calculates the BMI Category
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user