Copyright ©

Mongoose OS Forum

ATTENTION! This forum has moved to:

Do not post any new messages.

How to convert int to string?

I need to prepare a request string for Thingspeak MQTT API. The content of the published message needs to follow old Thingspeak GET request rules:
I want to fill the numeric part with values returned from mJS calls - for example,, or ESP32.temp() - those calls return FFI-ed int's.
Implicit conversion fails with error, ESP32.temp().toString() also does not work.
So I have no idea how to achieve this in the JS code. The only way that I see now is to create new C FFI functions that returns "char*" results - so I could make the conversion in the C using sprintf() or friends.
Is there something that I am missing? Maybe this could be achieved in some JS API lib, liks "string_utils.js" - but I am not that good at JS to create one myself.


  • SergeySergey Dublin, Ireland

    Use JSON.stringify()

  • Hi. Related to this question, what's the recommended way to convert a number to a string with a fixed number of digits after the decimal point?

  • mamuespmamuesp Germany/Northern coast

    JSON.stringify(Math.round(number * 100.0) / 100.0); e.g. for two digits.

    I'n not sure if Math.round() exists, you may use Math.floor() instead.

    Thanked by 1Sergey
  • Under Mongoose OS that gives results like "26.900000" or "27" - it successfully rounds, but the stringify() does not produce a fixed number of digits after the decimal point.

    (So far the only way I have to solve the problem is to produce two strings - one before and one after the decimal point - but considering the nature of many of the applications of Mongoose OS I feel that there must be a better solution that I've not thought of.)

  • SergeySergey Dublin, Ireland
    edited September 2018

    mjs does not provide toFixed . It probably should. Feel free to add.

    As a workaround, I think you can FFI sprintf and format the way you want.

    $ mjs -e 'let f = ffi("int sprintf(char *, char *, double)"); let buf="must be more than 5 chars"; let len = f(buf, "%.2f", 0.12345); buf.slice(0, len)'

    This works on my Mac, not sure if it'll work on a device.

    Thanked by 1CaptainNormal
  • mamuespmamuesp Germany/Northern coast
    edited October 2018

    @CaptainNormal - Sorry, I forgot to add the functionality to find the position of the decimal point with ìndexOf() and get the substring of the result in the length of the found position plus the wished number of digits via splice(). My bad ... :wink: I implemented a solution this way. But the FFIed method with `sprintf()' is more elegant, but you have to struggle with memory management. Might be handled, of course you have to be aware of the situation to provide a buffer in MJS anf free it after calling the FFIed function.

    @cpq - Thank you for the little example of using the MJS engine via CLI. Might be helpful when developping in sone cases! :smiley:

    Thanked by 1CaptainNormal
  • olafolaf Tempe AZ US
    edited March 2019

    Tried JSON.stringify(Math.round(number * 100.0) / 100.0);
    I am still getting 6 decimals on an ESP32 device??

Sign In or Register to comment.