Qt / C ++将QString转换为十进制 [英] Qt/C++ Convert QString to Decimal

查看:1941
本文介绍了Qt / C ++将QString转换为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将QString转换为十进制?

How Can I convert QString to decimal ?

在C#代码中,它如下所示:

In C# code it look like that:

public static decimal ConvertToDecimal(string tekst, bool upperOnly)
{
decimal num = 0m;
decimal num2 = 1m;
string text = upperOnly ? "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" : "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234";
int i = tekst.Length - 1;
while (i >= 0)
{
    num += text.IndexOf(tekst[i]) * num2;
    i--;
    num2 *= text.Length;
}
return num;
}


推荐答案

http://doc-snapshot.qt-project.org/qdoc/qstring.html#toInt =nofollow>文档:

As per documentation:


int QString :: toInt(bool * ok = 0,int base = 10)const

int QString::toInt(bool * ok = 0, int base = 10) const

返回使用基本库转换为int的字符串,默认为10,必须在2和36之间,或0.如果转换失败,则返回0.

Returns the string converted to an int using base base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.

如果发生转换错误,假;

If a conversion error occurs, *ok is set to false; otherwise *ok is set to true.

如果base为0,则使用C语言约定:如果字符串以0x开头,则使用base 16;如果字符串以0开头,则使用base 8;

If base is 0, the C language convention is used: If the string begins with "0x", base 16 is used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.

字符串转换将始终在C语言环境中进行。对于区域设置依赖转换使用QLocale :: toInt()

The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toInt()

示例:



QString str = "FF";
bool ok;
int hex = str.toInt(&ok, 16);       // hex == 255, ok == true
int dec = str.toInt(&ok, 10);       // dec == 0, ok == false

请注意,根据您的具体用例,也不妨查看以下文档:

Note that depending on your exact use case, you may wish to look into the following documentations as well:

long QString :: toLong(bool * ok = 0,int base = 10)const

qlonglong QString :: toLongLong(bool * ok = 0,int base = 10)const

double QString :: toDouble(bool * ok = 0)const

float QString :: toFloat(bool * ok = 0)const

这篇关于Qt / C ++将QString转换为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆