Converting string to double

Former Member
Former Member
Hi,
I want to convert a "string" number to double, but I'm getting an error.
Here is an example:

var x="1.0";
var p = x.toDouble();

The last code explode with the next error message:
Could not find symbol toDouble.
Symbol Not Found Error


But if I use toFloat() instead of toDouble it works ok.

var x="1.0";
var p = x.toFloat();


Any ideas to convert a string to double?

Thanks!
  • As you've noticed, Lang.String doesn't have a toDouble() method. If you don't mind the loss of precision, you can just write..

    s.toFloat().toDouble()


    If you care about precision, but you don't need to read the unusual formats (like 1.123e10), something like this should work...

    function string_toDouble(s) {

    var decimal_point = s.find(".");
    if (decimal_point == null) {
    return s.toNumber().toDouble();
    }

    var pfx = s.substring(0, decimal_point);
    var sfx = s.substring(decimal_point + 1, s.length());

    return pfx.toNumber() + sfx.toNumber() / Math.pow(10.0d, sfx.length());
    }

    function do_test_to_double(logger, s, e)
    {
    var r = string_toDouble(s);
    Test.assertMessage(r instanceof Lang.Double, "Expected a double");
    Test.assertEqualMessage(r, e, Lang.format("Converting '$1$' to double got $2$, expected $3$", [ s, r, e ]));
    }

    (:test) function test_to_double(logger)
    {
    do_test_to_double(logger, "123", 123.0d);
    do_test_to_double(logger, "123.123", 123.123d);
    do_test_to_double(logger, "1.123456789", 1.123456789d);

    return true;
    }


    Travis
  • hi

    ihave the same problem of Wearapps.

    I must convert string text in double format. in particulary numbers with 1 decimal (for example: 5.2) and register to Properties.

    function onTextEntered(text, changed) {
    var peso = text.toFloat.toDouble();
    Properties.setValue(DefaultPropertiesView.DOUBLE_PROP, peso );
    }

    in this way not works...

    how i can resolve ?

  • You need to use

    var peso = test.toFloat().toDouble();

  • I found an issue to add String.toLong() and String.toDouble() in our backlog (WERETECH-1728). This should be an easy fix, so I'll see what I can do about getting it added to the API.

  • return to me value always value+five zeros....5.200000

    this is Settings.xml:

    <layout id="SettingsLayout" background="Graphics.COLOR_BLACK">
    <label x="center" y="1" text="Settings GPSBike" font="Graphics.FONT_TINY" justification="Graphics.TEXT_JUSTIFY_CENTER" color="Graphics.COLOR_RED" />

    <label id="DoubleLabel" x="center" y="20" font="Graphics.FONT_TINY" justification="Graphics.TEXT_JUSTIFY_CENTER" />

    <label x="center" y="145" text="Premi Enter per modificare" font="Graphics.FONT_TINY" justification="Graphics.TEXT_JUSTIFY_CENTER" color="Graphics.COLOR_RED" />
    <label x="center" y="159" text="Premi Return per uscire" font="Graphics.FONT_TINY" justification="Graphics.TEXT_JUSTIFY_CENTER" color="Graphics.COLOR_RED" />
    </layout>

    this is Properties.xml:

    <resources>

    <properties>
    <property id="double_prop" type="double">2.1</property>
    </properties>

    <strings>
    <string id="double_title">Double</string>
    </strings>

    <settings>
    <setting propertyKey="@Properties.double_prop" title="@Strings.double_title">
    <settingConfig type="numeric" />
    </setting>
    </settings>
    </resources>

  • I think you're confusing a value and displaying a value.  Consider this:

    var str="5.2";
    var val=str.toFloat().toDouble();
    Sys.println("val="+val+" with format="+val.format("%0.1f"));

    What's output is:

    val=5.20000 with format=5.2

    When doing math, you'll want to use "val", but for display, you probably want to convert val to a string with a specific format.

    By the way, what is actually happening with the println, is that the

    "val="+val

    is in effect doing

    "val="+val.toString()

    to get a string to display

  • Yes Jim. Are first steps in MonkeyC. I'm beginner!! Slight smile

    For now i have resolved in this way for read value from Properties:

    var app = Application.getApp();
    var string = app.getProperty("string_prop");
    var peso2 = string.toFloat();

    and after for view:

    dc.drawText(120,170, Graphics.FONT_SMALL, peso2.format("%.01f") , Graphics.TEXT_JUSTIFY_CENTER);

    but now 'im not interested to view value. I need value (peso2) for use it in one formula.

    Thanks for infos!