9

假设我有一个大数字(整数或浮点数),例如12345,我希望它看起来像12,345

我将如何做到这一点?

我正在尝试为 iPhone 应用程序执行此操作,因此 Objective-C 或 C 中的某些内容会很好。

4

7 回答 7

31

这是答案。

  NSNumber* number = [NSNumber numberWithDouble:10000000];
  NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
  [numberFormatter setNumberStyle:kCFNumberFormatterDecimalStyle];
  [numberFormatter setGroupingSeparator:@","];
  NSString* commaString = [numberFormatter stringForObjectValue:number];
  [numberFormatter release];
  NSLog(@"%@ -> %@", number, commaString);
于 2009-06-22T05:59:03.863 回答
17

尝试使用NSNumberFormatter

这应该允许您在 iPhone 上正确处理此问题。不过,请确保使用 10.4+ 样式。从该页面:

“iPhone OS:iPhone OS 上没有 v10.0 兼容模式,只有 10.4 模式可用。”

于 2009-05-06T16:47:20.943 回答
3

至少在 Mac OS X 上,您可以只使用带有 printf(3) 的“'”字符串格式化程序。

$ man 3 printf

     `''          Decimal conversions (d, u, or i) or the integral portion
                  of a floating point conversion (f or F) should be
                  grouped and separated by thousands using the non-mone-
                  tary separator returned by localeconv(3).

如 printf("%'6d",1000000);

于 2009-05-07T04:07:57.203 回答
2

更清洁的 C 代码

// write integer value in ASCII into buf of size bufSize, inserting commas at tousands
// character string in buf is terminated by 0.
// return length of character string or bufSize+1 if buf is too small.
size_t int2str( char *buf, size_t bufSize, int val )
{
    char *p;
    size_t len, neg;

    // handle easy case of value 0 first
    if( val == 0 )
    {
         a[0] = '0';
         a[1] = '\0';
         return 1;
    }


    // extract sign of value and set val to absolute value
    if( val < 0 )
    {
        val = -val;
        neg = 1;
    }
    else
        neg = 0;

    // initialize encoding
    p = buf + bufSize;
    *--p = '\0';
    len = 1;

    // while the buffer is not yet full
    while( len < bufSize )
    {
         // put front next digit
         *--p = '0' + val % 10;
         val /= 10;
         ++len;

         // if the value has become 0 we are done
         if( val == 0 )
             break;

         // increment length and if it's a multiple of 3 put front a comma
         if( (len % 3) == 0 )
             *--p = ',';
   }

   // if buffer is too small return bufSize +1 
   if( len == bufSize && (val > 0 || neg == 1) )
       return bufSize + 1;

   // add negative sign if required
   if( neg == 1 )
   {
       *--p = '-';
       ++len;
   }

   // move string to front of buffer if required
   if( p != buf )
       while( *buf++ = *p++ );

   // return encoded string length not including \0
   return len-1;
}
于 2009-05-06T18:37:38.887 回答
1

我最近为 iPhone 游戏做了这个。我使用的是内置 LCD 字体,这是一种等宽字体。我格式化了数字,忽略了逗号,然后将逗号插入。(计算器这样做的方式,逗号不被视为字符。)

查看RetroJuJu的截图。抱歉——它们不是全尺寸的截图,所以你必须眯着眼睛!

于 2009-05-06T19:50:10.927 回答
0

希望对您有所帮助(它在 C 中):

char* intToFormat(int a)
{
    int nb = 0;
    int i = 1;
    char* res;

    res = (char*)malloc(12*sizeof(char));
    // Should be enough to get you in the billions. Get it higher if you need
    // to use bigger numbers.

    while(a > 0)
    {
        if( nb > 3 && nb%3 == 0)
            res[nb++] = ',';

        // Get the code for the '0' char and add it the position of the
        // number to add (ex: '0' + 5 = '5')
        res[nb] = '0' + a%10;

        nb++;
        a /= 10;
    }

    reverse(&res);
    return res;
}

可能有一些我没有看到的错误(我对此一无所知......)它就像一个增强的 iToA,所以它可能不是最好的解决方案。

于 2009-05-06T17:59:30.960 回答
0

使用递归,卢克:

#include <stdio.h>
#include <stdlib.h>

static int sprint64u( char* buffer, unsigned __int64 x) {
  unsigned __int64 quot = x / 1000;
  int chars_written;
  if ( quot != 0) {
    chars_written = sprint64u( buffer, quot);
    chars_written += sprintf( buffer + chars_written, ".%03u", ( unsigned int)( x % 1000));
  }
  else {
    chars_written = sprintf( buffer, "%u", ( unsigned int)( x % 1000));
  }
  return chars_written;
}
int main( void) {
  char buffer[ 32];
  sprint64u( buffer, 0x100000000ULL);
  puts( buffer);
  return EXIT_SUCCESS;
}
于 2009-05-07T11:04:28.747 回答