TWD 顯示改為整數(四捨五入到個位)

This commit is contained in:
Timmy
2026-04-17 13:12:58 +08:00
parent 6b7eab1095
commit 1f229da736
5 changed files with 7 additions and 11 deletions

Binary file not shown.

View File

@@ -86,10 +86,10 @@ private fun parseAmount(s: String): Double? =
s.filter { it != ',' && !it.isWhitespace() }.takeIf { it.isNotEmpty() }?.toDoubleOrNull()
private fun roundHalfUp(v: Double): BigDecimal =
BigDecimal.valueOf(v).setScale(2, RoundingMode.HALF_UP)
BigDecimal.valueOf(v).setScale(0, RoundingMode.HALF_UP)
private fun formatTwd(v: Double): String =
DecimalFormat("#,##0.00").format(roundHalfUp(v))
DecimalFormat("#,##0").format(roundHalfUp(v))
private fun formatTwdRaw(v: Double): String =
roundHalfUp(v).toPlainString()

Binary file not shown.

Binary file not shown.

View File

@@ -139,12 +139,9 @@ fn parse_amount(s: &str) -> Option<f64> {
}
fn format_twd(v: f64) -> String {
let rounded = (v * 100.0).round() / 100.0;
let sign = if rounded < 0.0 { "-" } else { "" };
let abs = rounded.abs();
let int_part = abs.trunc() as u64;
let frac = ((abs - int_part as f64) * 100.0).round() as u64;
let int_str = int_part.to_string();
let rounded = v.round() as i64;
let sign = if rounded < 0 { "-" } else { "" };
let int_str = rounded.unsigned_abs().to_string();
let mut with_sep = String::new();
for (i, ch) in int_str.chars().rev().enumerate() {
if i > 0 && i % 3 == 0 {
@@ -153,12 +150,11 @@ fn format_twd(v: f64) -> String {
with_sep.push(ch);
}
let int_sep: String = with_sep.chars().rev().collect();
format!("{sign}{int_sep}.{frac:02}")
format!("{sign}{int_sep}")
}
fn format_twd_raw(v: f64) -> String {
let rounded = (v * 100.0).round() / 100.0;
format!("{rounded:.2}")
format!("{}", v.round() as i64)
}
struct App {