Globalize.format custom numeric strings

Posted by: kamil.zainal.abidin on 18 October 2020, 8:55 pm EST

  • Posted 18 October 2020, 8:55 pm EST

    Hi,

    I'm looking to do a custom numeric format for accounting where negative numbers are encapsulated in brackets, amongst other things:
    -1234.567 -> (1,234.57)

    Looking at the documentation https://www.grapecity.com/wijmo/api/classes/wijmo.globalize.html#format I can see that Globalize.format is based off https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings.
    As far as I can tell, the library is capable of adding in the appropriate formatting for commas and decimal places, but there isn't any way to handle negative values enclosed in brackets.

    Is there any functionality similar to https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings, or would I need to write a custom formatter to handle this?

    Many thanks
  • Marked as Answer

    Replied 19 October 2020, 10:37 pm EST

    Hello Kamil,
    The specified bracket format gets applied for currency format. This format can be changed as you would like by modifying the wijmo.culture. Please refer to the following code snippet and the fiddle sample for reference:
    http://jsfiddle.net/e0qsvbpd/
    wijmo.culture.Globalize.numberFormat.currency.pattern[0]="-$n";


    Hope it helps!

    Regards,
    Manish Gupta
  • Replied 21 October 2020, 3:44 am EST

    Hi,

    Thanks for pointing out how we can change the pattern for currency - that definitely helped me sort out the negative and currency formats.

    One more question regarding the Globalize formats though. The docs state that the g general format uses the .NET numeric format strings. But it seems to me that adding the precision value doesn't actually format it to 15 significant digits, but instead makes it to 15 decimal places (see http://jsfiddle.net/9t14bqxa/).

    I understand that the library probably doesn't intend to copy the exact functionality done in the .NET formatter, so is there any easy way for me to override this functionality so I can make the 'g15' format to actually format a number to 15 significant digits?

    Many thanks
  • Replied 21 October 2020, 11:19 pm EST

    Hi,

    As stated in the following doc:
    https://www.grapecity.com/wijmo/api/classes/wijmo.globalize.html#formatnumber
    The number format in wijmo takes the "Axxsscc" form where "xx" xx is an optional integer called precision specifier.
    I understand that the confusion is arised from the fact that the docs mentioned that format accept the .NET-style Standard Numeric Format Strings. But it is just for the A part in the above-mentioned form. Actual functionality could be a little different.
    Further, I've asked the dev team to confirm if this is actually the excepted behavior or is it a bug(#467575). I will update you as soon as I hear back from the team.

    Regarding overriding the g formatter, you would have to override the Globalize.formatNumber method and return the required result for the 'g' formatter.

    Regards
  • Replied 25 October 2020, 9:49 pm EST

    Hi,

    Thanks for the clarification on the docs and pointing out the method to override.
    I look forward to finding out if it's as intended, but I suppose it's consistent with the other formats in treating 'xx' as the number of decimal points to show.

  • Replied 26 October 2020, 8:10 pm EST

    Hi,

    The dev team has confirmed that it is by design and is consistent with other formats too.

    Regards
  • Replied 2 November 2020, 9:03 pm EST

    Aha, okay. Thanks for the update.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels