How to convert string to double ?



  • I'm new to qml..
    Can anyone tell me how to convert String to double or long long value ?
    I mean I want a double or long long result. I'm writing an application for a large calculation...
    And want to add comma (,) after 3 digit and also need 5 precision.

    I mean I want something like --> 12,345,678.976548

    How can I do that...???

    @ TextInput {
    id: textInput1
    width: 146
    height: 37
    text: qsTr("Text Input")
    font.pixelSize: 12
    }

    TextInput {
        id: textInput2
        width: 142
        height: 37
        text: qsTr("Text Input")
        font.pixelSize: 12
    }
    
    Button {
        height: 30
        width: 100
        onClicked: {
            root.text = textInput1 + textInput2
        }
    }@


  • I don't know if you can do that, if you stay in pure javaScript you can only use float precision I guess:
    @
    parseFloat("12,345,678.976548".replace(",", ""))
    @
    As far as I know JS floats are always of 64 bit precision, that should be like a c++ double (long double) I guess?
    for your example that number should very well fit inside of a float, I don't know if you can use real c++ double or long long within QML, you can declare a double property in QML but there is no way of parsing a string to double I guess, so you might have to use your own c++ function for that.



  • Thank you for reply...:-)
    I've made something like
    @Rectangle {

    property real calc: textInput1.text
    property real calc2: textInput2.text
    property real result: calc + calc2
    
    id: window
    visible: true
    width: 360
    height: 360
    
    Button {
        id: button
        width: 200
        height: 30
        onClicked: { button.text = result }
    }
    
    TextInput {
        id: textInput1
        x: 124
        y: 99
        width: 80
        height: 20
        text: parseFloat("20")
        font.pixelSize: 12
    }
    
    TextInput {
        id: textInput2
        x: 124
        y: 181
        width: 80
        height: 20
        text: parseFloat("10")
        font.pixelSize: 12
    }
    

    }@

    and It works...
    But I'm still unable to add comma after 3 digit...



  • Ah sorry I misunderstood you, I thought you wanted to convert a string to a number, but if you want to add comma you want to convert a number to a string (numbers can't have commas of course, so you need a string to display the number as a formatted string). :)
    It makes no sense to do something like
    @
    text: parseFloat("20")
    @
    because that will convert the string "20" to a float and then convert it back to a string "20", which is the same as before obviously.

    there is no easy way of doing that I guess, you can maybe use a regular expression, I found this on "stackoverflow":http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript it looks like a good solution:
    @
    function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }
    @
    or you can do it by yourself, split the string and add commas, but the result will be the same :)

    Edit: Just got an idea, maybe you can also use QLocale and Qt's number formatting, since all you want is a thousand separator and QLocale::toString should be able to do that for you, but you need c++ for that, don't think that can be done from QML.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.