Viewed   66 times

I have this string :

000000000000100

and need to convert it to:

1,00

So, the rules are:

  1. Divide the number by 100 and use a comma as decimal separator
  2. Strip leading zeros
  3. Keep two decimals

 Answers

3

From the PHP Manual page on number_format:

string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

If you want numbers like 123456 be formatted as 1234,45, use:

echo number_format($number / 100, 2, ",", "");

If you need a dot as thousands separator (1.234,56):

echo number_format($number / 100, 2, ",", ".");

The zeros are automatically removed by PHP when converting the string to a number.

Wednesday, November 30, 2022
3

You can use number-format. For example :

echo number_format($number, 2, ',', ' ');

But you will have to put the unit yourself.

Otherwise, this comment of the money_format documentation page gives a version of the function with some code that you can modify to change the thousand separator.

You can also do something like this :

$locale = localeconv();
$text = money_format('%i', $number);
$text = str_replace($locale['mon_thousands_sep'], ' ', $text);
Monday, October 10, 2022
 
karim_h
 
5

http://userguide.icu-project.org/datetime/timezone#TOC-Factory-Methods-and-the-Default-Tim says

TimeZone maintains a static time zone object known as the default time zone. This is the time zone that is used implicitly when the user does not specify one. ICU attempts to match this to the host OS time zone.

In short, if you want to change the default timezone from intl to match what date() says, you must change the time zone on your operating system. But don't do that.

It is preferred that you specify the timezone in the call to IntlDateFormatter::create(). If you wish to use the default timezone that PHP is using elsewhere, that can be retrieved with date_default_timezone_get().

$dateFormater = IntlDateFormatter::create(
    'en_EN',
    IntlDateFormatter::LONG,
    IntlDateFormatter::NONE,
    date_default_timezone_get()
);
Saturday, December 24, 2022
2

This will help you

Format (xxx) xxx-xxxx

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    int length = (int)[self getLength:textField.text];
    //NSLog(@"Length  =  %d ",length);

    if(length == 10)
    {
        if(range.length == 0)
            return NO;
    }

    if(length == 3)
    {
        NSString *num = [self formatNumber:textField.text];
        textField.text = [NSString stringWithFormat:@"(%@) ",num];

        if(range.length > 0)
            textField.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];
    }
    else if(length == 6)
    {
        NSString *num = [self formatNumber:textField.text];
        //NSLog(@"%@",[num  substringToIndex:3]);
        //NSLog(@"%@",[num substringFromIndex:3]);
        textField.text = [NSString stringWithFormat:@"(%@) %@-",[num  substringToIndex:3],[num substringFromIndex:3]];

        if(range.length > 0)
            textField.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]];
    }

    return YES;
}

- (NSString *)formatNumber:(NSString *)mobileNumber
{
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

    NSLog(@"%@", mobileNumber);

    int length = (int)[mobileNumber length];
    if(length > 10)
    {
        mobileNumber = [mobileNumber substringFromIndex: length-10];
        NSLog(@"%@", mobileNumber);

    }

    return mobileNumber;
}

- (int)getLength:(NSString *)mobileNumber
{
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

    int length = (int)[mobileNumber length];

    return length;
}
Sunday, September 18, 2022
 
2

So eventually found the answer after a lot of research.

PHP only really works with standard US decimal seperators and can't cope with the french , seperator.

The answer, although not perfect is to reset numeric values to use US while allowing dates etc to be formatted in French. Placing this line:

 setlocale(LC_NUMERIC, 'C');

Under

 setlocale(LC_ALL, 'fr_FR'); 

has done the trick

Saturday, September 24, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :