如何将iOS计算结果以十进制格式显示而非科学计数法?
I get exactly what you're dealing with here—your current code is just using string interpolation ("\(arshin)") to turn those tiny Double values into strings, which defaults to scientific notation for really small numbers. The NumberFormatter you set up was only being used to parse the user's input text into a number, not to format the output values. That's why your initial change didn't do anything!
Here's how to fix this properly:
Step 1: Create an Output-Specific NumberFormatter
You need a formatter configured to output decimal notation instead of scientific. Add this right after you calculate all the converted values in your doneClicked() method:
// Set up a formatter for outputting decimal values let outputFormatter = NumberFormatter() outputFormatter.locale = Locale.current outputFormatter.numberStyle = .decimal outputFormatter.usesScientificNotation = false // Critical: Disable scientific notation outputFormatter.minimumFractionDigits = 10 // Show enough decimal places for tiny values outputFormatter.maximumFractionDigits = 15 // Limit to a reasonable number of digits outputFormatter.usesSignificantDigits = false
Step 2: Use the Formatter to Set Your Label Texts
Instead of using direct string interpolation, use the formatter to convert each Double to a properly formatted string. We'll add a fallback to the default string just in case the formatter can't handle the value (though that's unlikely here):
// Replace all your label assignments with this pattern arshinLabel.text = outputFormatter.string(from: NSNumber(value: arshin)) ?? "\(arshin)" verstaLabel.text = outputFormatter.string(from: NSNumber(value: versta)) ?? "\(versta)" vershokLabel.text = outputFormatter.string(from: NSNumber(value: vershok)) ?? "\(vershok)" decimetrLabel.text = outputFormatter.string(from: NSNumber(value: decimetr)) ?? "\(decimetr)" duimLabel.text = outputFormatter.string(from: NSNumber(value: duim)) ?? "\(duim)" kilometrLabel.text = outputFormatter.string(from: NSNumber(value: kilometr)) ?? "\(kilometr)" metrLabel.text = outputFormatter.string(from: NSNumber(value: metr)) ?? "\(metr)" miliLabel.text = outputFormatter.string(from: NSNumber(value: mili)) ?? "\(mili)" millimetrLabel.text = outputFormatter.string(from: NSNumber(value: millimetr)) ?? "\(millimetr)" mormiliLabel.text = outputFormatter.string(from: NSNumber(value: mormil)) ?? "\(mormil)" sajenLabel.text = outputFormatter.string(from: NSNumber(value: sajen)) ?? "\(sajen)" santimetrLabel.text = outputFormatter.string(from: NSNumber(value: santimetr)) ?? "\(santimetr)" futLabel.text = outputFormatter.string(from: NSNumber(value: fut)) ?? "\(fut)" yardLabel.text = outputFormatter.string(from: NSNumber(value: yard)) ?? "\(yard)"
Why Your Previous Attempts Didn't Work
- The
formatter.numberStyle = .decimalline only affected how the input text was parsed, not how the output was displayed. Direct string interpolation ignores that formatter entirely. - When you tried changing
let number = formatter.number(from: text)tolet number = formatter.string(from: number), you had a variable name conflict (you were trying to usenumberbefore it was defined) and that method is meant to convert numbers to strings, not parse input text—so that logic was backwards.
内容的提问来源于stack exchange,提问作者Ivan Kisin

