Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift how to convert Decimal type to String type

Tags:

decimal

swift

How to convert Decimal to String in swift?

For example

let de = Decimal(string: "123")

then how to convert de to String.

like image 241
William Hu Avatar asked Aug 10 '17 10:08

William Hu


2 Answers

Leveraging the fact that Decimal conforms to protocol CustomStringConvertible I would simply do:

let decimalString = "\(de)"
like image 167
Marco Avatar answered Sep 23 '22 11:09

Marco


Convert to NSDecimalNumber and use the stringValue.

NSDecimalNumber(decimal: de).stringValue

like image 34
Chase Finch Avatar answered Sep 26 '22 11:09

Chase Finch