Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to convert string to double ?

How to convert string to double ?

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 16.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    x3ex
    wrote on last edited by
    #1

    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
        }
    }@
    
    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xander84
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • X Offline
        X Offline
        x3ex
        wrote on last edited by
        #3

        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...

        1 Reply Last reply
        0
        • X Offline
          X Offline
          Xander84
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved