Форматирование (большого) номера “12345” к “12 345”

Вы указываете Content-Type в качестве строки заголовка.

Предполагая, что вы добавляете свои заголовки со стандартной нотацией точек, вы будете использовать:

$headers .= "Content-Type: multipart/mixed";

И позже это станет четвертым параметром вашей функции mail():

mail($address, $subject, $message, $headers);
[ 118] Вот основной пример:

$address = 'sample@sample.com';
$subject = 'Subject';
$message = 'Message';
$headers = "From: My Name<something@something.com>\n";
$headers .= "Reply-To: something@something.com \n";
$headers .= "Content-Type: multipart/mixed";

mail($address, $subject, $message, $headers);
9
задан Svante 6 May 2009 в 16:44
поделиться

7 ответов

Попробуйте использовать an NSNumberFormatter .

Это должно позволить вам правильно обрабатывать это на iPhone. Убедитесь, что вы используете стиль 10.4+. С этой страницы:

«iPhone OS: режим совместимости v10.0 недоступен в iPhone OS - доступен только режим 10.4».

17
ответ дан 4 December 2019 в 05:59
поделиться

По крайней мере, в 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);

3
ответ дан 4 December 2019 в 05:59
поделиться

Код очистителя 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;
}
2
ответ дан 4 December 2019 в 05:59
поделиться

Hope that helps you (it's in 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;
}

There might be a few errors I didn't see (I'm blind when it comes to this...) It's like an enhanced iToA so maybe it's not the best solution.

0
ответ дан 4 December 2019 в 05:59
поделиться

Использовать рекурсию, Люк:

#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;
}
0
ответ дан 4 December 2019 в 05:59
поделиться

Недавно я сделал это для игры для iPhone. Я использовал встроенный ЖК-шрифт, это моноширинный шрифт. Я отформатировал числа, игнорируя запятые, а потом вставил запятые. (Так, как это делают калькуляторы, где запятая не считается символом.)

Посмотрите скриншоты на RetroJuJu . Извините, это не полноразмерные скриншоты, поэтому вам придется прищуриться!

1
ответ дан 4 December 2019 в 05:59
поделиться

Вот ответ.

  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);
31
ответ дан 4 December 2019 в 05:59
поделиться
Другие вопросы по тегам:

Похожие вопросы: