I have this string :
000000000000100
and need to convert it to:
1,00
So, the rules are:
- Divide the number by 100 and use a comma as decimal separator
- Strip leading zeros
- Keep two decimals
I have this string :
000000000000100
and need to convert it to:
1,00
So, the rules are:
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);
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()
);
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;
}
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
From the PHP Manual page on
number_format
:If you want numbers like
123456
be formatted as1234,45
, use:If you need a dot as thousands separator (
1.234,56
):The zeros are automatically removed by PHP when converting the string to a number.