2

I'm trying to format the integer returned for UInt64.max to add commas to the number. In case it matters, I'm currently writing this out in a "Playground" and I'm using XCode 6.3.2.

Here's the relevant code:

//: Playground - noun: a place where people can play

import UIKit

// MARK: - Integer Bounds

var formatter = NSNumberFormatter()
formatter.numberStyle = .DecimalStyle

println( formatter.stringFromNumber(100000) )
println( formatter.stringFromNumber(100000000) )
println( formatter.stringFromNumber( NSNumber(char: Int8.min) ) )

println( Int8.min )
println( Int8.max )

println( UInt8.min )
println( UInt8.max )

println( Int16.min )
println( formatter.stringFromNumber( NSNumber(short: Int16.min) ) )
println( Int16.max )
println( formatter.stringFromNumber( NSNumber(short: Int16.max) ) )

println( UInt16.min )
println( UInt16.max )
println( formatter.stringFromNumber( NSNumber(unsignedShort: UInt16.max) ) )

println( Int32.min )
println( formatter.stringFromNumber( NSNumber(int: Int32.min) ) )
println( Int32.max )
println( formatter.stringFromNumber( NSNumber(int: Int32.max) ) )

println( UInt32.min )
println( UInt32.max )
println( formatter.stringFromNumber( NSNumber(unsignedInt: UInt32.max) ) )

println( Int64.min )
println( formatter.stringFromNumber( NSNumber(longLong: Int64.min) ) )
println( Int64.max )
println( formatter.stringFromNumber( NSNumber(longLong: Int64.max) ) )

println( UInt64.min ) // 0
println( UInt64.max ) // 18446744073709551615
println( formatter.stringFromNumber( NSNumber(unsignedLongLong: UInt64.max) ) ) // Optional("-1")

All of these return a properly formatted number except the last one (UInt64.max).

As shown, the last line (where I try to use formatter to add the commas) returns Optional("-1").

How could I print UInt64.max formatted with commas?

Ezra Free
  • 808
  • 11
  • 21
  • Looks like a bug to me. It seems that every number > Int64.max is formatted as a negative number. – Martin R Jun 10 '15 at 22:33
  • My question here may be a duplicate of this one: http://stackoverflow.com/questions/24218918/why-is-uint64-max-equal-1-in-swift – Ezra Free Jun 11 '15 at 03:55

1 Answers1

0

In Obj-C (just in case):

NSNumber *num = [NSNumber numberWithDouble:18446744073709551615.];

NSNumberFormatter *formatter;
formatter = [[NSNumberFormatter alloc] init];

formatter.numberStyle = kCFNumberFormatterDecimalStyle;
[formatter setMaximumFractionDigits:0];
[formatter stringFromNumber:num];

NSString *formattedString = [formatter stringFromNumber:num];

NSLog(@"%@", formattedString);

Results 18,446,744,073,709,600,000

In Swift

    var num = 18446744073709551615.0

    var formatter = NSNumberFormatter()
    formatter.numberStyle = .DecimalStyle
    formatter.maximumFractionDigits = 0
    formatter.stringFromNumber(num)

    println(formatter.stringFromNumber(num))

Results Optional("18,446,744,073,709,600,000")

mondousage
  • 483
  • 1
  • 5
  • 14
  • 2
    `18,446,744,073,709,600,000` is not really the expected output :) A `Double` cannot represent `UInt64.max` precisely. – Martin R Jun 10 '15 at 23:16
  • 1
    Sorry, but I am hoping to print the value of `UInt64.max` formatted with commas, not to set the number `18446744073709551615` into a variable and print that variable formatted with commas. – Ezra Free Jun 11 '15 at 13:47
  • Converting to double changed the value regardless. Int overflow issues. Whoops! – mondousage Jun 11 '15 at 16:41