Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Decimal number format

mladjomladjo Posts: 42Registered Users
edited July 2010 in iPhone SDK Development
Does anyone knows how to print some integer in american format, with coma as a separator?

For example : 100000 as 100,000

for now I am using this for integer to string conversion:
[NSString stringWithFormat:@"%dms",[md.averageResponseTime intValue]/1000];
Post edited by mladjo on

Replies

  • PhoneyDeveloperPhoneyDeveloper Posts: 1,431Registered Users
    edited October 2008
  • mladjomladjo Posts: 42Registered Users
    edited October 2008
    Thx a lot. That was the solution. If someone is looking for a quick fix, here is the snippet :)
    //American number format - with comma sepparators between thousands
    NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
    [numberFormatter setFormat:@"#,###"];
    NSNumber *averageResponseTime = [NSNumber numberWithInt:123456789];
    
    //If you print next thing
    [NSString stringWithFormat: @"%@ms",[numberFormatter stringForObjectValue:averageResponseTime]];
    
    //You will get 123,456,789
    

    Cheers
  • henryj11khenryj11k Posts: 2New Users
    edited February 2009
    Hi I tried the above to display commas as seperators for thousands and it works fine in the iPhone simulator but not on my iTouch.....ugh. and ideas?

    Thanks
  • JoeBlazeJoeBlaze Posts: 97Registered Users
    edited March 2009
    Hey I ran into the same problem try this...
    		NSNumberFormatter *frm = [[NSNumberFormatter alloc] init];
    		[frm setNumberStyle:NSNumberFormatterCurrencyStyle];
    		[frm setCurrencySymbol:@""];
    		[frm setMaximumFractionDigits:0];
    		NSString *formatted = [frm stringFromNumber:[NSNumber numberWithInt:100000]];
    

    The setFormat: is not supported on iPhone for some unknown reason but that should take care of what you need. For a number with decimal values you can change the value in [frm setMaximumFractionDigits:0]
  • snehalsnehal Posts: 6New Users
    edited March 2009
    Hi,
    I also ran into similar kind of problem few days ago...
    As JoeBlaze stated setFormat is not supported on iphone...works fine on simulator but not for iphone..

    This is what i had done to solve the problem
    NSNumberFormatter *lengthFormatter;
    NSNumber *score=[NSNumber numberWithInt:levelScore];
    [lengthFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    lblScore.text=[lengthFormatter stringForObjectValue: score]];
    

    And it worked for me on iphone ;)
  • henryj11khenryj11k Posts: 2New Users
    edited March 2009
    Hi Guys, Thanks for the two suggestions. I need some beginner level help.
    If my current formatter is:

    NewAmount.text = [NSString stringWithFormat:@"%2.2f", amount];

    In the code that you both provided where do I put the input (amount) and the output (NewAmount.text)?

    Many thanks in advance......

    Henry

    snehal wrote: »
    Hi,
    I also ran into similar kind of problem few days ago...
    As JoeBlaze stated setFormat is not supported on iphone...works fine on simulator but not for iphone..

    This is what i had done to solve the problem
    NSNumberFormatter *lengthFormatter;
    NSNumber *score=[NSNumber numberWithInt:levelScore];
    [lengthFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    lblScore.text=[lengthFormatter stringForObjectValue: score]];
    

    And it worked for me on iphone ;)
  • snehalsnehal Posts: 6New Users
    edited March 2009
    Here is where the variable will go...
    NSNumberFormatter *lengthFormatter;
    NSNumber *score=[NSNumber numberWithInt: amount];
    [lengthFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    NewAmount.text=[lengthFormatter stringForObjectValue: score]];
    
    henryj11k wrote: »
    Hi Guys, Thanks for the two suggestions. I need some beginner level help.
    If my current formatter is:

    NewAmount.text = [NSString stringWithFormat:@"%2.2f", amount];

    In the code that you both provided where do I put the input (amount) and the output (NewAmount.text)?

    Many thanks in advance......

    Henry
  • HaoshiroHaoshiro Posts: 1New Users
    edited May 2009
    snehal wrote: »
    NSNumberFormatter *lengthFormatter;
    NSNumber *score=[NSNumber numberWithInt:levelScore];
    [lengthFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    lblScore.text=[lengthFormatter stringForObjectValue: score]];
    

    That code will cause your app to crash because it's not allocating/initializing the formatter object.

    Use this instead:
    NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
    NSNumber *score = [[NSNumber alloc] initWithInt: levelScore];
    [numFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
    lblScore.text = [numFormatter stringFromNumber: score];
    
  • sendelsendel Posts: 2New Users
    edited September 2009
    Does any one knoe how to set the ThousandsSeperator on iPhone / iPod Touch Devices?

    lets say I have a value : 123456
    and my output should be something like :
    SCORE: 000-123-456
    or
    SCORE: 000.123.456

    This is what google told me to do:
    [NSFormatter setThousandSeperator:@"-"];

    but unfortunatelly this doesn't work on iPhone OS because there is a code line :
    if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
    
    which prevents it from beeing used on iPhones...

    Bummer!

    so, I hacked a work around, which helps me to continue coding... but I want to fix this Hack before I release the app.

    my current code is :
    + (NSString *) ScoreFormatter:(uint) score
    {
    	NSNumberFormatter * numberFormat = [[NSNumberFormatter alloc]init];
    	[numberFormat setPositiveFormat:@"SCORE: 000,000,000"];
    	
    	NSNumber * nsNumber = [[NSNumber alloc] initWithInt:score];
    	NSString * str = [numberFormat stringFromNumber:nsNumber];
    	
    	[numberFormat release];
    	[nsNumber release];
    	
    	
    	NSMutableString *mstring = [NSMutableString stringWithString:str];	
    	NSRange wholeShebang = NSMakeRange(0, [str length]);
    	[mstring replaceOccurrencesOfString:@"," withString:@"." options:0 range: wholeShebang];
    	
    	return [NSString stringWithString: mstring];
    }
    

    so NSFormatter would give me (hopefully) something like 000,123,456 and I replace "," by "." or "-"... but if some one has a diffrent localisation, this method would fail due to missing "," (like in germany they have "." and in other countries there might be nothing.)

    [NSFormatter setThousandSeperator:] does not function on iPhone OS - so how to fix this glitch?

    I would apprechiate any help!
    thanx in advance,
    sendel.

    PS:
    Haoshiro wrote: »
    That code will cause your app to crash because it's not allocating/initializing the formatter object.

    Use this instead:
    NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
    NSNumber *score = [[NSNumber alloc] initWithInt: levelScore];
    [numFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
    lblScore.text = [numFormatter stringFromNumber: score];
    

    You should also release the numFormatter. Otherwise frequent calls to this formatter thing (like updating your score) would also crash your application due to memory leaks.

    Add this at the bottom of the above code to fix this:
    [numFormatter release];
    

    Always think of no1 rule : "If I call alloc, I have to call release, too."
  • Pradeep Reddy KypaPradeep Reddy Kypa Posts: 6New Users
    edited November 2009
    snehal wrote: »
    Hi,
    I also ran into similar kind of problem few days ago...
    As JoeBlaze stated setFormat is not supported on iphone...works fine on simulator but not for iphone..

    This is what i had done to solve the problem
    NSNumberFormatter *lengthFormatter;
    NSNumber *score=[NSNumber numberWithInt:levelScore];
    [lengthFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    lblScore.text=[lengthFormatter stringForObjectValue: score]];
    

    And it worked for me on iphone ;)

    If we implement the code the text field is taking only 4 values and when the 5th button is pressed it is again starting with the 5th value but the comma is getting displayed properly. PLese help me out and i want to enter multiple values till 16
  • Pradeep Reddy KypaPradeep Reddy Kypa Posts: 6New Users
    edited November 2009
    snehal wrote: »
    Hi,
    I also ran into similar kind of problem few days ago...
    As JoeBlaze stated setFormat is not supported on iphone...works fine on simulator but not for iphone..

    This is what i had done to solve the problem
    NSNumberFormatter *lengthFormatter;
    NSNumber *score=[NSNumber numberWithInt:levelScore];
    [lengthFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    lblScore.text=[lengthFormatter stringForObjectValue: score]];
    

    And it worked for me on iphone ;)

    If we implement the code the text field is taking only 4 values and when the 5th button is pressed it is again starting with the 5th value but the comma is getting displayed properly. PLese help me out and i want to enter multiple values till 16
  • funk_ynot_24funk_ynot_24 Posts: 5New Users
    edited February 2010
    Hello JoeBlaze, I tried your code, but my value is 999999999999999999.99 .I change the data type with NSDecimalNumber, but when I want to display it as string, the value always rounded to 1000000000000000000.00

    My expectation is the result string show original value (not rounded value). Can you help me to solve this problem?
    JoeBlaze wrote: »
    Hey I ran into the same problem try this...
    		NSNumberFormatter *frm = [[NSNumberFormatter alloc] init];
    		[frm setNumberStyle:NSNumberFormatterCurrencyStyle];
    		[frm setCurrencySymbol:@""];
    		[frm setMaximumFractionDigits:0];
    		NSString *formatted = [frm stringFromNumber:[NSNumber numberWithInt:100000]];
    

    The setFormat: is not supported on iPhone for some unknown reason but that should take care of what you need. For a number with decimal values you can change the value in [frm setMaximumFractionDigits:0]
  • boehmrboehmr Posts: 5New Users
    edited July 2010
    I'm pulling my hair out. :)

    I'm trying to format the number with thousands separator AND decimal WITHOUT it rounding off my decimals (x,xxx.xx). Any help would be great!
    NSNumberFormatter *numberformatterdec = [[NSNumberFormatter alloc] init];
    [numberformatterdec setNumberStyle:NSNumberFormatterCurrencyStyle];
    [numberformatterdec setCurrencySymbol:@""];
    [numberformatterdec setMaximumFractionDigits:2];
    
    labelGalReq.text = [numberformatterdec stringFromNumber:[NSNumber numberWithInt:xgalreq]];
    
Sign In or Register to comment.