Coder Social home page Coder Social logo

Comments (2)

jeffleeismyhero avatar jeffleeismyhero commented on August 20, 2024

It seems that this could be an implementation option that applies to all fields OR as a per-field option. I propose that the option live in the JSON file(s) to allow quick and easy changes based on a user's locale or the context in an application.

I'm not familiar with how iOS handles different locales or if it is a viable option for this project. This seems to be a well written resource on the matter: http://useyourloaf.com/blog/2012/06/14/using-number-formatters.html

Example: Device language set as English

NSNumber *number = [NSNumber numberWithDouble:1234567.8369];
NSString *numberString = @"";

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterNoStyle];
NSLog(@"NSNumberFormatterNoStyle: %@", numberString);
// => NSNumberFormatterNoStyle: 1234568

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterDecimalStyle];
NSLog(@"NSNumberFormatterDecimalStyle: %@", numberString);
// => NSNumberFormatterDecimalStyle: 1,234,567.837

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterCurrencyStyle];
NSLog(@"NSNumberFormatterCurrencyStyle: %@", numberString);
// => NSNumberFormatterCurrencyStyle: $1,234,567.84

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterPercentStyle];
NSLog(@"NSNumberFormatterPercentStyle: %@", numberString);
// => NSNumberFormatterPercentStyle: 123,456,784%

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterScientificStyle];
NSLog(@"NSNumberFormatterScientificStyle: %@", numberString);
// => NSNumberFormatterScientificStyle: 1.2345678369E6

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterSpellOutStyle];
NSLog(@"NSNumberFormatterSpellOutStyle: %@", numberString);
// => NSNumberFormatterSpellOutStyle: one million two hundred thirty-four thousand five hundred sixty-seven point eight three six nine

Example: Device language set as Norwegian

NSNumber *number = [NSNumber numberWithDouble:1234567.8369];
NSString *numberString = @"";

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterNoStyle];
NSLog(@"NSNumberFormatterNoStyle: %@", numberString);
// => NSNumberFormatterNoStyle: 1234568

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterDecimalStyle];
NSLog(@"NSNumberFormatterDecimalStyle: %@", numberString);
// => NSNumberFormatterDecimalStyle: 1 234 567,837

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterCurrencyStyle];
NSLog(@"NSNumberFormatterCurrencyStyle: %@", numberString);
// => NSNumberFormatterCurrencyStyle: USD 1 234 567,84

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterPercentStyle];
NSLog(@"NSNumberFormatterPercentStyle: %@", numberString);
// => NSNumberFormatterPercentStyle: 123 456 784 %

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterScientificStyle];
NSLog(@"NSNumberFormatterScientificStyle: %@", numberString);
// => NSNumberFormatterScientificStyle: 1,2345678369E6

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterSpellOutStyle];
NSLog(@"NSNumberFormatterSpellOutStyle: %@", numberString);
// => NSNumberFormatterSpellOutStyle: én million tohundre­og­fire­og­tredve tusen og femhundre­og­syv­og­seksti komma åtte tre seks ni

Example: Device region set as Norway (language English)

NSNumber *number = [NSNumber numberWithDouble:1234567.8369];
NSString *numberString = @"";

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterNoStyle];
NSLog(@"NSNumberFormatterNoStyle: %@", numberString);
// => NSNumberFormatterNoStyle: 1234568

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterDecimalStyle];
NSLog(@"NSNumberFormatterDecimalStyle: %@", numberString);
// => NSNumberFormatterDecimalStyle: 1 234 567,837

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterCurrencyStyle];
NSLog(@"NSNumberFormatterCurrencyStyle: %@", numberString);
// => NSNumberFormatterCurrencyStyle: NOK 1 234 567,84

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterPercentStyle];
NSLog(@"NSNumberFormatterPercentStyle: %@", numberString);
// => NSNumberFormatterPercentStyle: 123 456 784%

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterScientificStyle];
NSLog(@"NSNumberFormatterScientificStyle: %@", numberString);
// => NSNumberFormatterScientificStyle: 1,2345678369E6

numberString = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterSpellOutStyle];
NSLog(@"NSNumberFormatterSpellOutStyle: %@", numberString);
// => NSNumberFormatterSpellOutStyle: one million two hundred thirty-four thousand five hundred sixty-seven point eight three six nine

Alternatively, you could set the region at the app level by creating and implementing custom formatters. This may provide an easy way to change the grouping and decimal separators on demand.

Example: Custom formatter

// Custom formatter
NSNumber *number = [NSNumber numberWithDouble:1234567.8369];
NSString *numberString = @"";
NSNumberFormatter *customFormatter = [[NSNumberFormatter alloc] init];
[customFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[customFormatter setGroupingSeparator:@" "];
[customFormatter setDecimalSeparator:@","];
numberString = [customFormatter stringFromNumber:number];
NSLog(@"NSNumberFormatterDecimalStyle (custom formatter): %@", numberString);
// => NSNumberFormatterDecimalStyle (custom formatter): 1 234 567,837

from form.

aminiz avatar aminiz commented on August 20, 2024

Is this also addressed by #521

from form.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.