Forum Discussion

Henrik's avatar
Henrik
Advisor
11 years ago

Endpoints and numbers

Hi,

I have an important issue with endpoints and numbers. All numbers above 1000 are transformed in 1 000 with a space between "1" and "000". So when I want to make simple rest calls with a number (user.id for instance), it fails when it's higher than 999.


Example: I want the last messages written by the user whose id is 6207.

"/users/id/${user.id}/posts/latest" or "/users/id/" + user.id + "/posts/latest"

will give

"/users/id/6 207/posts/latest" instead of "/users/id/6207/posts/latest"

I've also tested it with the freemarker '?number' function. It also fails to give 1000 without space.

 

Meanwhile, I've discovered this article: 
https://lithosphere.lithium.com/t5/developers-discussion/user-id-number-format-issue/m-p/112673#M4135 

Which was close to my issue. So I tried user.id?c and it worked well! 

But everything is a bit messy in my head. 
For instance, if I want to transform a string to a number, should I use "?number?c" or just "?c" ? 
Why ?number and int don't behave the same way in endpoints than in custom components? 

Thanks for your help! 
Henrik

6 Replies

  • PaoloT's avatar
    PaoloT
    Lithium Alumni (Retired)
    11 years ago

    Hi Henrik 

     

    when it comes to numbering, dates etc ... all becomes very relative to the specific locale you are considering. So for example in some countries a certain number could be written in a specific format with a different decimal or thousdands separator etc ... 

     

    If you intend to use a number programmatically - then it's recommended to use the ?c built in to indicate Freemarker that the value is not meant to be "human readable" ( see documentation - where it's explained in better words than mine! )

     

    Does this make sense?

     

     

     

     

  • ChiaraS's avatar
    ChiaraS
    Lithium Alumni (Retired)
    11 years ago

    DougS are endpoints handling freemarker objects in a different way? I agree with Paolo that we should use "?c" to be on the safe side, but it's still weird to me that ${user.id} returns a different value inside an endpoint compared to a custom component (I checked on another instance and I get "330,940" from the endpoint and "330940" in the component)...

  • nathan's avatar
    nathan
    Executive
    11 years ago

    It is a bit odd. I assume it's picking up a different localisation rules for some reason.

     

    Thinking about it, custom components always reside within the community user interface, so you'd expect them to inherit the community's localisation settings (including number format). End points on the other hand are designed more for APIs, and therefore are more location agnostic by design.

  • PaoloT's avatar
    PaoloT
    Lithium Alumni (Retired)
    11 years ago

    I have the same understanding as nathan - but let's wait for a confirmation on the subject.

  • Henrik's avatar
    Henrik
    Advisor
    11 years ago

    Hi,

     

    Thank you all for your answers! And thanks @PaoloT for the documentation.

     

    So basically, when I cast a number into a string, the normal behaviour is "1 000" or "1,000" depending on the local settings.

    I should always add a "?c" when I use a number into a string for programming purposes.

     

    I guess I will have to check all my custom components then. As ${mynumber?number} was working fine, I never had to use the "?c" function.

     

    Henrik

  • nathan's avatar
    nathan
    Executive
    11 years ago

    ?number converts a string to a number (see http://freemarker.org/docs/ref_builtins_string.html#ref_builtin_number)

     

    ?c converts a number to a computer-readable string (see http://freemarker.org/docs/ref_builtins_number.html#ref_builtin_c)

     

    Numbers can't be displayed, or treated as a string, without first being converted to a string.If you try to include a number in the output of a custom component/endpoint, FreeMarker will automatically try to convert it to a string, using location-based rules.

     

    FreeMarker provides a few different functions to let you convert a number to a string whilst controlling the format. These are all documented on this page:

    http://freemarker.org/docs/ref_builtins_number.html

     

    user.id is a number, not a string, so it needs to be converted to a string before you can include it in the output, or use it within another string (like when creating REST API URLs).