Home forums Report Bugs Number Formatting: Decimal Point after scaled number with suffix

This topic contains 5 replies, has 2 voices, and was last updated by  Sunil Urs 2 years, 8 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #6115

    michael_t
    Participant

    Hi,

    I think I found a bug in the Number Formatting functionality:

    If I use the ‘,’ character for number scaling by inserting it in front of the Decimal Point and add some chars without further meaning to the end of the valueFormatString, the value is always printed with a Decimal Point, even if it’s unnecessary. Means:

    3000 (“#,##0,.#”) -> 3
    3000 (“#,##0k”) -> 3000k
    but
    3000 (“#,##0,.#k”) -> 3.k

    in my opinion it should result in “3k”.

    You can reproduce this behaviour in both versions of CanvasJS (1.3 and 1.4 Beta) and in this small JSFiddle I made up: http://jsfiddle.net/QwZuf/50/

    With kind regards,
    Michael T.

    #6116

    michael_t
    Participant

    Okay,
    I just took a look at the source code and managed to fix the problem by myself.

    Line 1079 (in canvasjs.js of version 1.4) says up to now:
    result += (charCount++ === 0 ? decimalSeparator : "") + match;

    I changed it to the following:
    result += ((charCount++ === 0 && vStringAfterDecimal.length > 0 && Number(vStringAfterDecimal.join("")) !== 0 && noPhAfterDecimal) ? decimalSeparator : "") + match;

    This will cause the number formatting function to add the decimalSeparator on non-placeholder-characters only if one or more placeholders will follow AND the value to show after decimal separator is greater than 0. I hope this solution won’t cause any other issues.

    So, maybe you could include this fix into the main release.

    With kind regards,
    Michael T.

    PS: By the way: Thanks for the great work! 🙂

    #6117

    michael_t
    Participant

    Mhm.

    What I presented above is still not a perfect solution. It disregards the possibility of zeros in the part after the decimal separator.

    This could be corrected by an additional variable (and two additional lines of code) that counts the number of zeros after the decimal separator.

    With kind regards,
    Michael T.

    #6119

    Sunil Urs
    Keymaster

    Michael,

    Here is an intermediate build with the fix. Please let me know if its working as expected.


    Sunil Urs

    #6122

    michael_t
    Participant

    Thanks for your quick response.

    This is a solution and should solve the issues to my knowledge.
    For every format string I could test yet, it actually does.

    Michael T.

    #6124

    Sunil Urs
    Keymaster

    We are glad that it solved the problem. It’ll be a part of the next release.


    Sunil Urs

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.