修正 Kotlin 四捨五入(原本 kotlin.math.round 是銀行家捨入,改用 BigDecimal HALF_UP)
This commit is contained in:
@@ -47,8 +47,11 @@ import androidx.compose.ui.unit.sp
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.math.BigDecimal
|
||||
import java.math.RoundingMode
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
import java.text.DecimalFormat
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
@@ -82,13 +85,14 @@ private fun fetchRates(): List<Rate> {
|
||||
private fun parseAmount(s: String): Double? =
|
||||
s.filter { it != ',' && !it.isWhitespace() }.takeIf { it.isNotEmpty() }?.toDoubleOrNull()
|
||||
|
||||
private fun formatTwd(v: Double): String {
|
||||
val rounded = kotlin.math.round(v * 100.0) / 100.0
|
||||
return "%,.2f".format(rounded)
|
||||
}
|
||||
private fun roundHalfUp(v: Double): BigDecimal =
|
||||
BigDecimal.valueOf(v).setScale(2, RoundingMode.HALF_UP)
|
||||
|
||||
private fun formatTwd(v: Double): String =
|
||||
DecimalFormat("#,##0.00").format(roundHalfUp(v))
|
||||
|
||||
private fun formatTwdRaw(v: Double): String =
|
||||
"%.2f".format(kotlin.math.round(v * 100.0) / 100.0)
|
||||
roundHalfUp(v).toPlainString()
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
|
||||
Reference in New Issue
Block a user