Operations with QString and int
-
It's not entirely clear, but from the wording of the original post (increase the last digit by one) and then in a subsequent message saying it should work with strings that contain floating-point numbers too, I think @Swati777999 wants '35.456' to become '35.457', for example.
@mchinand said in Operations with QString and int:
It's not entirely clear, but from the wording of the original post (increase the last digit by one) and then in a subsequent message saying it should work with strings that contain floating-point numbers too, I think @Swati777999 wants '35.456' to become '35.457', for example.
Yes, actually my true intention for putting this Question was about dealing with rounding of real values but I also wanted to be clear about the string and int conversion.
-
@Swati777999 said in Operations with QString and int:
Yes, it works absolutely fine. I was trying this with real values say - myStr = " 314.19"
Please again, don't program in C++ as you would do it in python, this will not work!
There is no implicit convertion from string <=> real or string <=> integer in C++.
I you want to program in python style, use python not C++ or you will only go frustrated.@KroMignon
My current project requirement expects me to code in Qt C++ [ As I've mentioned earlier that my mind always approaches the solution of a program in Python first because of spending more time in coding in Python than C++] -
@Swati777999 said in Operations with QString and int:
With this modified code , I got the following result-
It is doing what you have coded.
QString myStr = "31415"; int myStr_sz = myStr.size(); // => myStr = 5 int last_dig=myStr[myStr_sz-1].digitValue(); // => last_dig = 5 last_dig+=1; /// => last_dig = 6 myStr[myStr_sz-1] = last_dig; // change myStr[myStr_sz-1] from '5' to '\x06' qDebug()<<"New String is =" <<myStr;EDIT: you have to understand that 5 != '5', the ASCII code for 5 is 57 or '\x35'
@KroMignon said in Operations with QString and int:
@Swati777999 said in Operations with QString and int:
With this modified code , I got the following result-
It is doing what you have coded.
QString myStr = "31415"; int myStr_sz = myStr.size(); // => myStr = 5 int last_dig=myStr[myStr_sz-1].digitValue(); // => last_dig = 5 last_dig+=1; /// => last_dig = 6 myStr[myStr_sz-1] = last_dig; // change myStr[myStr_sz-1] from '5' to '\x06' qDebug()<<"New String is =" <<myStr;EDIT: you have to understand that 5 != '5', the ASCII code for 5 is 57 or '\x35'
EXAMPLE-1int x = 56; QString str = QString::number(x); qDebug()<<"x = " <<str; // x ="56"Using Example 1 -
I modified the code``` QString myStr = "31415"; qDebug()<<"old String is =" <<myStr; //31415 int myStr_sz = myStr.size(); // => myStr = 5 int last_dig=myStr[myStr_sz-1].digitValue(); // => last_dig = 5 last_dig+=1; // => last_dig = 6 qDebug()<<"incremented last dig is="<<last_dig; // 6 QString str_last =QString::number(last_dig); qDebug()<<"incremented last dig after converting to string is="<<last_dig; // ==> 6 myStr[myStr_sz-1] = last_dig; qDebug()<<"New String is =" <<myStr;
I am getting the same old result.
Another way I'm thinking is that, popping out the last digit from the string and appending the incremented last digit . I tried it but didn't work. Please give suggestion .
-
@Swati777999 said in Operations with QString and int:
You are assigning ASCII value of 6 to the last character of string. Please refer to ASCII chart online. To simply answer your question, you can add '0' to ur last_dig. That should work.
myStr[myStr_sz-1] = last_dig + '0';But why are u doing like this? What if the last digit is 9? When u add 1, you will need to add the second last digit as well. A better solution would be to completely convert the string to digit, do the arithmetric operation, and then convert it back to QString
-
@KroMignon
My current project requirement expects me to code in Qt C++ [ As I've mentioned earlier that my mind always approaches the solution of a program in Python first because of spending more time in coding in Python than C++]@Swati777999 said in Operations with QString and int:
My current project requirement expects me to code in Qt C++ [ As I've mentioned earlier that my mind always approaches the solution of a program in Python first because of spending more time in coding in Python than C++]
So we are totally in opposite about the way to do: programming C++ but thinking the program with Python in mind is, for me, the worst way to do.
C++ and python are so far apart in almost every aspect of programming, I can't see how this can work.
If we are only talking about validating an algorithm, why not. But transposing from Python to C++ is not a simple matter. That alone amounts to a new development. -
@Swati777999 said in Operations with QString and int:
You are assigning ASCII value of 6 to the last character of string. Please refer to ASCII chart online. To simply answer your question, you can add '0' to ur last_dig. That should work.
myStr[myStr_sz-1] = last_dig + '0';But why are u doing like this? What if the last digit is 9? When u add 1, you will need to add the second last digit as well. A better solution would be to completely convert the string to digit, do the arithmetric operation, and then convert it back to QString
@Sivan said in Operations with QString and int:
@Swati777999 said in Operations with QString and int:
You are assigning ASCII value of 6 to the last character of string. Please refer to ASCII chart online. To simply answer your question, you can add '0' to ur last_dig. That should work.
myStr[myStr_sz-1] = last_dig + '0';But why are u doing like this? What if the last digit is 9? When u add 1, you will need to add the second last digit as well. A better solution would be to completely convert the string to digit, do the arithmetric operation, and then convert it back to QString
Yes, your suggestion works perfectly well.
QString myStr = "31415"; qDebug()<<"old String is =" <<myStr; int myStr_int = myStr.toInt(); myStr_int +=1; qDebug()<<"New int value is =" <<myStr_int;
Trying with real vlaues
QString myStr = "99.99"; qDebug()<<"old String is =" <<myStr; int myStr_int = myStr.toDouble(); // And:100 [.toFloat() ] myStr_int +=1; qDebug()<<"New int value is =" <<myStr_int; -
@Swati777999 said in Operations with QString and int:
You are assigning ASCII value of 6 to the last character of string. Please refer to ASCII chart online. To simply answer your question, you can add '0' to ur last_dig. That should work.
myStr[myStr_sz-1] = last_dig + '0';But why are u doing like this? What if the last digit is 9? When u add 1, you will need to add the second last digit as well. A better solution would be to completely convert the string to digit, do the arithmetric operation, and then convert it back to QString
@Sivan said in Operations with QString and int:
@Swati777999 said in Operations with QString and int:
You are assigning ASCII value of 6 to the last character of string. Please refer to ASCII chart online. To simply answer your question, you can add '0' to ur last_dig. That should work.
myStr[myStr_sz-1] = last_dig + '0';But why are u doing like this? What if the last digit is 9? When u add 1, you will need to add the second last digit as well. A better solution would be to completely convert the string to digit, do the arithmetric operation, and then convert it back to QString
Here's my try with real values
QString myStr = "99.20"; qDebug()<<"old String is =" <<myStr; int indx = myStr.indexOf("."); myStr.remove('.'); float myStr_int = myStr.toFloat(); //100 myStr_int +=1; QString myStrNew = QString::number(myStr_int); myStrNew.insert(indx,"."); qDebug()<<"New int value is =" <<myStrNew.toFloat();
-
The output 99.21 is correct don't know why you do such strange stuff just to add 0.01 to a float value though)
-
The output 99.21 is correct don't know why you do such strange stuff just to add 0.01 to a float value though)
@Christian-Ehrlicher for some reason, I was not getting the correct result at first, this is why I opted for the first method in the first place. Now, I realize that more than coding, I have to be cautious about what I am displaying on the screen. I also, have to avoid my tendency to arrive at any conclusion so quickly.
-
As I said - I don't understand what you're doing. Your stuff will e.g. not work for 99.99...