Is there a simple way to format currency into string in iOS?

前端 未结 7 1092

I need a way to format the price from NSNumber into a string like this: \"USD 0.99\", not \"$ 0.99\".

My game uses custom fonts, and they could not have the symbols

7条回答
  •  臣服心动
    2020-12-23 16:00

    The – productsRequest:didReceiveResponse: method gives you back a list of SKProducts.

    Each product contains a property priceLocale which contains the local currency of the product for the current user.

    You could use the following sample code (apple's) to format it:

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [numberFormatter setLocale:product.priceLocale];
    NSString *formattedString = [numberFormatter stringFromNumber:product.price];
    

    Good luck!

提交回复
热议问题