Swift - Negative string to integer conversion not being parsed correctly

Stack Overflow Asked by methodical on December 16, 2021

This is a fairly simple question but has been causing me a multitude of issues. I have a integer which comes from my API endpoint in a string format, as this: "-43.47" (for example). I need to display the number differently (as they’re percentages of change) in my SwiftUI view. The code I have been trying to use for this is below;

if((Int(self.percentageChange) ?? 0) >= 0) {
   Text("+ " + String(self.percentageChange + "%")).foregroundColor(
} else {
   Text("- " + String((0 - Int(self.percentageChange)) + "%")).foregroundColor(

The problem is, whether or not the string number is a negative integer, it gets parsed as a positive integer and appears as so in my actual view (without an actual photo, but take my word for it): "+ -43.47%", and I have no idea why. Obviously -43.47 is less than 0, so it should be caught by the exception, but for some strange reason, perhaps as a result of my application of parenthesis or otherwise, it isn’t being caught as such.

3 Answers

Use NumberFormatter:

let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .percent
numberFormatter.multiplier = 1
numberFormatter.maximumFractionDigits = 2
numberFormatter.positivePrefix = "+"


let percentageChange = Double(self.percentageChange) ?? 0
let color = percentageChange < 0 ? :

Text(numberFormatter.string(for: percentageChange)).foregroundColor(color)

Note that this will make your number formatted correctly and properly localized.

Answered by Sulthan on December 16, 2021

Isn't it easier to check the string for the leading minus sign

if self.percentageChange.hasPrefix("-") {
   Text(self.percentageChange + "%").foregroundColor(
} else {
   Text("+ " + self.percentageChange + "%").foregroundColor(

Answered by vadian on December 16, 2021

You just need to use the Float initializer to convert floating-point string to float and then compare.


if((Int(self.percentageChange) ?? 0) >= 0) {


if((Float(percentageChange) ?? 0) >= 0) {

The better alternative is to do this:

if !self.percentageChange.hasPrefix("-") {

Answered by Frankenstein on December 16, 2021

Add your own answers!

Related Questions

Can not put a T in Map

1  Asked on November 25, 2020 by hemant


If and excel formula for matching

1  Asked on November 23, 2020 by howaboutno


Pyspark Transpose

1  Asked on November 23, 2020 by sawan-s


How to aggregate data, pass and fail as final result?

1  Asked on November 23, 2020 by user584018


Comparing two lists for same value

1  Asked on November 22, 2020 by sebaku


Crystal Reports won’t display related table data

2  Asked on November 22, 2020 by rharris


Upgrading spring cloud version from Edgware to Hoxton

3  Asked on November 21, 2020 by ruchira-gayan-ranaweera


How to prevent button form Angular?

5  Asked on November 20, 2020 by user13976004


SQL union seems to fail for me

1  Asked on November 19, 2020 by sasha-peric


My Projectiles Stop Shooting Problem How To Fix?

1  Asked on November 19, 2020 by habib-ismail


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP