iOS Question Specify thousands separator

Lucas Eduardo

Active Member
Licensed User
Hello,
I did not find anything like this in B4I forum.

Is there a way to do the same thing in B4I?

Thanks
 

JordiCP

Well-Known Member
Licensed User
Try this :)

NumberFormat3:
Sub NumberFormat3(num As Double, groupSeparator As String, decimalSeparator As String, maxFractions As Int) As String
    Dim no As NativeObject = Me
    Dim objRes As Object = no.RunMethod("NumberFormat3::::",Array(num,groupSeparator,decimalSeparator,maxFractions))
    Dim res As String = objRes ' Will cast it directly to String
    Return res
End Sub

#if OBJC
// From: https://stackoverflow.com/questions/10103270/how-to-use-thousand-separator-in-nsstring

-(NSString*)NumberFormat3: (double)num : (NSString*)groupSeparator :(NSString*)decimalSeparator :(int)numFractionDigits {
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    //[numberFormatter setGroupingSeparator:@","];
    [numberFormatter setGroupingSeparator:groupSeparator];
    [numberFormatter setGroupingSize:3];
    [numberFormatter setUsesGroupingSeparator:YES];
    [numberFormatter setDecimalSeparator:decimalSeparator];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [numberFormatter setMaximumFractionDigits:numFractionDigits];//2
    NSString *theString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:num]];
    return theString;
}
#End If
Usage example:
B4X:
    Dim myNum as Double = 123456.789012
    Log($"Formating ${myNum} result is: '${NumberFormat3(myNum,",",".",1)}'"$)
    
    ' Log output should be --> Formating 123456.789012 result is: '123,456.8'
(In the source SO code there is also a way to format according to locale settings, the code above is to format with chosen group and decimal separators)
 

Lucas Eduardo

Active Member
Licensed User
It works, but when i put numFractionDigits = 2 it's not appling two digits after comma if there is a zero in the final number.
Example:
B4X:
Log($"Formating ${1222.50} result is: '${NumberFormat3(1222.50,".",",",2)}'"$)
It's returning 1.222,5 without zero, if number has like 1.222,55 works good, with zero the code cuts the zero.

What could be?

Thank you.
 

José J. Aguilar

Well-Known Member
Licensed User
Maybe this could be useful? Not sure if it works on iOS

 

JordiCP

Well-Known Member
Licensed User
(Untested, but should work)

Objective-C:
-(NSString*)NumberFormat3: (double)num : (NSString*)groupSeparator :(NSString*)decimalSeparator :(int)numFractionDigits 
{
   NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
   [numberFormatter setGroupingSeparator:groupSeparator];
   [numberFormatter setGroupingSize:3];
   [numberFormatter setUsesGroupingSeparator:YES];
   [numberFormatter setDecimalSeparator:decimalSeparator];
   [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
   [numberFormatter setMinimumFractionDigits:numFractionDigits];      // <--------- ADD THIS LINE
   [numberFormatter setMaximumFractionDigits:numFractionDigits]; 
   NSString *theString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:num]];
   return theString;
}
 

Erel

Administrator
Staff member
Licensed User
Cross platform code:
B4X:
Dim formatter As B4XFormatter 'B4XFormatter internal library
formatter.Initialize
Dim df As B4XFormatData = formatter.GetDefaultFormat
df.DecimalPoint = ","
df.GroupingCharacter = "."
df.MaximumFractions = 2
df.MinimumFractions = 2
Log(formatter.Format(1222.5))
Make formatter a global variable and use it whenever you need.
 
Top