Convert NSDecimalNumber to String

You could try some of these options. They all should return 10. And also check why would you need to create NSString formatting the number and casting it to String.

"\(price)"
String(describing: price)
NSString(format: "%@", price)

NSDecimalValue inherits from NSNumber.

NSNumber have stringValue property

var stringValue: String { get }

The number object's value expressed as a human-readable string.
The string is created by invoking description(withLocale:) where locale is nil.

Documentation Source


Two ways:

  1. use NumberFormatter

  2. use stringValue property directly

Code using NumberFormatter fixed to two decimal places:

Swift 5

let number = NSDecimalNumber(string: "1.1")
print(number.stringValue) //"1.1"

 let fmt = NumberFormatter()
  fmt.numberStyle = .none;
  fmt.minimumFractionDigits = 2;
  fmt.minimumIntegerDigits = 1;
  fmt.roundingMode = .halfUp;

let result = fmt.string(from: number) ?? "0.00"  
//1.10