33 lines
1.0 KiB
Dart
33 lines
1.0 KiB
Dart
/// Utility functions for gold/silver purity factor calculation and formatting.
|
|
/// Standard rule: Purity is represented as a fraction between 0.0 and 1.0 (e.g. 0.916 for 22KT / 91.6%).
|
|
/// If a raw value > 1.0 is supplied (such as 91.6 or 75.0), it is normalized by dividing by 100 with a maximum of 1.0.
|
|
|
|
double normalizePurity(num? rawPurity) {
|
|
if (rawPurity == null || rawPurity <= 0) return 1.0;
|
|
double p = rawPurity.toDouble();
|
|
if (p > 1.0) {
|
|
p = p / 100.0;
|
|
}
|
|
if (p > 1.0) {
|
|
p = 1.0;
|
|
}
|
|
return p;
|
|
}
|
|
|
|
String formatPurity(num? rawPurity) {
|
|
final p = normalizePurity(rawPurity);
|
|
return p.toStringAsFixed(3);
|
|
}
|
|
|
|
/// Resolves purity factor prioritizing the category (metal grade e.g. 22KT -> 0.916)
|
|
/// then falling back to product-specific purity or 1.0.
|
|
double resolvePurity({num? categoryPurity, num? productPurity}) {
|
|
if (categoryPurity != null && categoryPurity > 0) {
|
|
return normalizePurity(categoryPurity);
|
|
}
|
|
if (productPurity != null && productPurity > 0) {
|
|
return normalizePurity(productPurity);
|
|
}
|
|
return 1.0;
|
|
}
|