1

I have a problem similar to the one in this question: How to obtain an unformatted string representation of an NSDecimal or NSDecimalNumber? I need to have a number in string format accurately represented as an NSNumber, but this number is converted back to a string in other places, and I can't avoid that. The problem I'm having is that when the number is converted back to a string, the string is in scientific notation in some cases.

NSDecimalNumber *dn = [NSDecimalNumber decimalNumberWithString:@"0.0001"];
NSString *s = [dn stringValue]; // s will be @"1E-4"

How can I prevent this number from being displayed in scientific notation?

I am working in a circa 2005 version of GNUstep (possibly v1.11.1 for GNUstep Base), rather than on mac or iPhone, which may account for some of the differences in behavior vs the other question I referenced. I have looked over the base library API and the documentation in my own GNUstep install, but I can't seem to find anything to help.

EDIT 2/7/12:

The question has changed slightly, but the goal is still the same. Originally, I didn't think I was able to control the string output piece, but I can pass the value back as a string. At this point I am attempting to use a formatting string, but I still want to prevent the scientific notation from appearing.

[NSString stringWithFormat:@"%1.14g", [val doubleValue]]

I've chosen to use %g because we would like a specific number of significant digits for the value. If I use %f, I can trim the extra zeros, but the number does not always come out cleanly. 800000000.79 appears as 800000000.7899999600, for example.

Is there a way to get a cleanly formatted number with up to a certain number of significant digits (or decimal places) without displaying scientific notation before that number of digits?

I'm willing to accept C advice as well.

Community
  • 1
  • 1
Jay Sheridan
  • 668
  • 8
  • 15

4 Answers4

2

You should check out the NSNumberFormatter

// Create formatter 
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];     
[formatter setNumberStyle:NSNumberFormatterDecimalStyle]; // adjust this
NSString *formattedOutput = [formatter stringFromNumber:yourDecimalNumber];
Sonny Saluja
  • 7,193
  • 2
  • 25
  • 39
  • It looks like in whatever version of GNUstep we're using (1.11.1 maybe?), the NSNumberFormatter class isn't fully implemented. The docs contain this message: "This class is currently not implemented in GNUstep! All set methods will work, but stringForObject: will ignore the format completely. The documentation below describes what the behavior SHOULD be..." – Jay Sheridan Jan 23 '12 at 15:42
1

How about getting the the C value (double, int, long, etc.) and then format it as a C string, or as an NSString with stringWithFormat:?

Monolo
  • 18,205
  • 17
  • 69
  • 103
0

Use the below methods to avoid scientific exponent notation representation.

Convert Double to String

func getStringFrom(double doubleVal: Double) -> String
{
    var stringValue : String = "0.00"

    let formatter = NSNumberFormatter()
    formatter.usesSignificantDigits = true;
    formatter.maximumSignificantDigits = 100
    formatter.groupingSeparator = "";
    formatter.numberStyle = .DecimalStyle
    stringValue = formatter.stringFromNumber(doubleVal)!;

    return stringValue

}

Convert String to Double

func getDoubleFrom(textField textField: UITextField) -> Double
{
    var doubleValue : Double = 0.0

    if let val = textField.text
    {
        let numberFormatter = NSNumberFormatter()
        numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
        let finalNumber = numberFormatter.numberFromString(val)
        doubleValue = (finalNumber?.doubleValue)!;
    }

    return doubleValue
}
0

Try printing the number using NSNumberFormatter instead of the stringValue method. It has a lot more options.

(I'm assuming NSNumberFormatter is available on GNUstep)

Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103