You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将iOS计算结果以十进制格式显示而非科学计数法?

Fixing Scientific Notation Display for Unit Conversion Results in Swift

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 = .decimal line 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) to let number = formatter.string(from: number), you had a variable name conflict (you were trying to use number before 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:44:36