Hello. Excuse me for asking you a question.
If the value is a number that follows "0" such as 0.00 when converting Double to String type, then
It will be a floating-point notation like "e-".
Does anyone know how to avoid this...
import Foundation
leta = 0.138274823423
letb = 0.000021312312
let at = String(a)
let bt = String(b)
print(at)//0.138274823423
print(bt)//2.1312312e-05
I thought I could use NumberFormatter, so I tried the following, but it didn't work.
let formatter=NumberFormatter()
formatter.numberStyle=.decimal
letans=formatter.string (from:NSNumber(value:b))
print(ans!)//0
You can also use String(format:)
as in self-answer, but you can do the same with NumberFormatter
.
let formatter=NumberFormatter()
formatter.numberStyle=.decimal
// Specify the number of digits here
formatter.minimumFractionDigits = 8
letans=formatter.string (from:bas NSNumber)
print(ans!)//->0.00002131
Conversion with String(format:)
does not take localization into consideration, so
You should limit your use only when you say that
Apple's class reference is not very easy to use because there are still few descriptions and few examples of code, but I'll show you just in case.
NumberFormatter
minimumFractionDigits
I'm sorry.It's self-solving. > People <
letb=0.000021318312
// Specify the number of digits here
letans=String (format: "%.8f", b)
print(ans) // 0.00002132
Please refer to the following
© 2024 OneMinuteCode. All rights reserved.