本文整理汇总了C++中decimals函数的典型用法代码示例。如果您正苦于以下问题:C++ decimals函数的具体用法?C++ decimals怎么用?C++ decimals使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了decimals函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: QString
QString BitcoinUnits::format(int unit, const CAmount& nIn, bool fPlus, SeparatorStyle separators)
{
// Note: not using straight sprintf here because we do NOT want
// localized number formatting.
if(!valid(unit))
return QString(); // Refuse to format invalid unit
qint64 n = (qint64)nIn;
qint64 coin = factor(unit);
int num_decimals = decimals(unit);
qint64 n_abs = (n > 0 ? n : -n);
qint64 quotient = n_abs / coin;
qint64 remainder = n_abs % coin;
QString quotient_str = QString::number(quotient);
QString remainder_str = QString::number(remainder).rightJustified(num_decimals, '0');
/*Right-trim excess zeros after the decimal point*/
int nTrim = 0;
for (int i = remainder_str.size()-1; i>=2 && (remainder_str.at(i) == '0'); --i)
++nTrim;
remainder_str.chop(nTrim);
// Use SI-style thin space separators as these are locale independent and can't be
// confused with the decimal marker.
QChar thin_sp(THIN_SP_CP);
int q_size = quotient_str.size();
if (separators == separatorAlways || (separators == separatorStandard && q_size > 4))
for (int i = 3; i < q_size; i += 3)
quotient_str.insert(q_size - i, thin_sp);
if (n < 0)
quotient_str.insert(0, '-');
else if (fPlus && n > 0)
quotient_str.insert(0, '+');
return quotient_str + QString(".") + remainder_str;
}
示例2: decimals
bool BitcoinUnits::parse(int unit, const QString &value, qint64 *val_out)
{
if(!valid(unit) || value.isEmpty())
return false; // Refuse to parse invalid unit or empty string
int num_decimals = decimals(unit);
QStringList parts = value.split(".");
if(parts.size() > 2)
{
return false; // More than one dot
}
QString whole = parts[0];
QString decimals;
if(parts.size() > 1)
{
decimals = parts[1];
}
if(decimals.size() > num_decimals)
{
return false; // Exceeds max precision
}
bool ok = false;
QString str = whole + decimals.leftJustified(num_decimals, '0');
if(str.size() > 18)
{
return false; // Longer numbers will exceed 63 bits
}
qint64 retvalue = str.toLongLong(&ok);
if(val_out)
{
*val_out = retvalue;
}
return ok;
}
示例3: decimals
int ValueSlider::
decimals(qreal r) const
{
int d = decimals();
r = fabs(r);
if (r>0)
{
while(r<0.099)
{
r*=10;
d++;
}
while(r>=1.1 && d>0)
{
r/=10;
d--;
}
}
return d;
}
示例4: decimals
bool BitcoinUnits::parse(int unit, const QString &value, qint64 *val_out)
{
if(!valid(unit) || value.isEmpty())
int num_decimals = decimals(unit);
QStringList parts = value.split(".");
if(parts.size() > 2)
{
}
QString whole = parts[0];
QString decimals;
if(parts.size() > 1)
{
decimals = parts[1];
}
if(decimals.size() > num_decimals)
{
}
bool ok = false;
QString str = whole + decimals.leftJustified(num_decimals, '0');
if(str.size() > 18)
{
}
qint64 retvalue = str.toLongLong(&ok);
if(val_out)
{
*val_out = retvalue;
}
return ok;
}
示例5: while
//.........这里部分代码省略.........
fieldSize( (uint16) tgt );
if( chr == '*' )
{
fixedSize( true );
}
else {
// reparse current char
--pos;
}
state = e_sInitial;
}
break;
//=============================
// Decimals parsing state
//
case e_sDecimals:
if( chr >= '0' && chr <= '9' )
{
tmp += chr;
// size too wide
if ( tmp.length() > 2 ) {
m_convType = e_tError;
return false;
}
}
else
{
int64 tgt;
tmp.parseInt( tgt );
decimals( (uint8) tgt );
// reparse current char
--pos;
state = e_sInitial;
}
break;
//===============================================
// Parsing what should be done in case of error.
//
case e_sErrorEffect:
if ( chr == 'c' )
{
state = e_sErrorEffect2;
break;
}
// else
switch( chr )
{
case 'n': mismatchAction( e_actNil ); break;
case '0': mismatchAction( e_actZero ); break;
case 'r': mismatchAction( e_actRaise ); break;
default:
// invalid choiche
m_convType = e_tError;
return false;
}
state = e_sInitial;
break;
示例6: textFromValue
QString textFromValue(double value) const
{
return QLocale().toString(value, 'f', decimals());
}
示例7: valueAsString
QString ValueSlider::
valueAsString(bool forceDisableUnits) const
{
return QString("%1%2").arg(value_,0,'f',decimals(value_)).arg (unit_.isEmpty () || hasFocus () || forceDisableUnits?"":" " + unit_);
}
示例8: decimals
bool BitcoinUnits::parse(int unit, const QString &value, int nColorIn, qint64 *val_out)
{
if(!valid(unit, nColorIn) || value.isEmpty())
return false; // Refuse to parse invalid unit or empty string
int num_decimals = decimals(unit, nColorIn);
QStringList parts = value.split(".");
if(parts.size() > 2)
{
if (fDebug)
{
printf("BitcoinUnits::parse: More than one dot!\n");
}
return false; // More than one dot
}
QString whole = parts[0];
QString decimals;
if(parts.size() > 1)
{
bool fNonzero = false;
for (int i = 0; i < parts[1].size(); ++i)
{
if (parts[1][i] != '0')
{
fNonzero = true;
break;
}
}
if (fNonzero)
{
decimals = parts[1];
}
}
if(decimals.size() > num_decimals)
{
if (fDebug)
{
printf("BitcoinUnits::parse: Size %d exceeds # decimals %d\n",
decimals.size(), num_decimals);
}
return false; // Exceeds max precision
}
bool ok = false;
QString str = whole + decimals.leftJustified(num_decimals, '0');
if (!ok)
{
if (fDebug)
{
printf("BitcoinUnits::parse: Couldn't left justify '%s'\n",
str.toUtf8().constData());
}
}
if(str.size() > 18)
{
if (fDebug)
{
printf("BitcoinUnits::parse: String size (%d) too big\n",
str.size());
}
return false; // Longer numbers will exceed 63 bits
}
qint64 retvalue = str.toLongLong(&ok);
if (!ok)
{
if (fDebug)
{
printf("BitcoinUnits::parse: Couldn't convert to LL '%s'\n",
str.toUtf8().constData());
}
}
if(val_out)
{
*val_out = retvalue;
}
return ok;
}
示例9: setRange
void QDoubleValidator::setTop(double top)
{
setRange(bottom(), top, decimals());
}
示例10: xbrlProcessFacts
RcppExport SEXP xbrlProcessFacts(SEXP epaDoc) {
xmlDocPtr doc = (xmlDocPtr) R_ExternalPtrAddr(epaDoc);
xmlXPathContextPtr context = xmlXPathNewContext(doc);
xmlXPathObjectPtr fact_res = xmlXPathEvalExpression((xmlChar*) "//*[@*[local-name()='contextRef']]", context);
xmlNodeSetPtr fact_nodeset = fact_res->nodesetval;
xmlXPathFreeContext(context);
int fact_nodeset_ln = fact_nodeset->nodeNr;
CharacterVector elementId(fact_nodeset_ln);
CharacterVector contextId(fact_nodeset_ln);
CharacterVector unitId(fact_nodeset_ln);
CharacterVector fact(fact_nodeset_ln);
CharacterVector decimals(fact_nodeset_ln);
CharacterVector sign(fact_nodeset_ln);
CharacterVector scale(fact_nodeset_ln);
CharacterVector tupleRef(fact_nodeset_ln);
CharacterVector order(fact_nodeset_ln);
CharacterVector factId(fact_nodeset_ln);
CharacterVector ns(fact_nodeset_ln);
for (int i=0; i < fact_nodeset_ln; i++) {
xmlNodePtr fact_node = fact_nodeset->nodeTab[i];
if (fact_node->ns->prefix)
elementId[i] = (char *) ((string) (char *) fact_node->ns->prefix + "_" + (string) (char *) fact_node->name).data();
else
elementId[i] = (char *) fact_node->name;
xmlChar *tmp_str;
if ((tmp_str = xmlGetProp(fact_node, (xmlChar*) "contextRef"))) {
contextId[i] = (char *) tmp_str;
xmlFree(tmp_str);
} else {
contextId[i] = NA_STRING;
}
if ((tmp_str = xmlGetProp(fact_node, (xmlChar*) "unitRef"))) {
unitId[i] = (char *) tmp_str;
xmlFree(tmp_str);
} else {
unitId[i] = NA_STRING;
}
if ((tmp_str = xmlNodeListGetString(doc, fact_node->xmlChildrenNode, 1))) {
fact[i] = (char *) tmp_str;
xmlFree(tmp_str);
} else {
fact[i] = NA_STRING;
}
if ((tmp_str = xmlGetProp(fact_node, (xmlChar*) "decimals"))) {
decimals[i] = (char *) tmp_str;
xmlFree(tmp_str);
} else {
decimals[i] = NA_STRING;
}
if ((tmp_str = xmlGetProp(fact_node, (xmlChar*) "scale"))) {
scale[i] = (char *) tmp_str;
xmlFree(tmp_str);
} else {
scale[i] = NA_STRING;
}
if ((tmp_str = xmlGetProp(fact_node, (xmlChar*) "sign"))) {
sign[i] = (char *) tmp_str;
xmlFree(tmp_str);
} else {
sign[i] = NA_STRING;
}
if ((tmp_str = xmlGetProp(fact_node, (xmlChar*) "tupleRef"))) {
sign[i] = (char *) tmp_str;
xmlFree(tmp_str);
} else {
sign[i] = NA_STRING;
}
if ((tmp_str = xmlGetProp(fact_node, (xmlChar*) "order"))) {
sign[i] = (char *) tmp_str;
xmlFree(tmp_str);
} else {
sign[i] = NA_STRING;
}
if ((tmp_str = xmlGetProp(fact_node, (xmlChar*) "id"))) {
factId[i] = (char *) tmp_str;
xmlFree(tmp_str);
} else if ((tmp_str = xmlGetProp(fact_node, (xmlChar*) "name"))) {
factId[i] = (char *) tmp_str;
xmlFree(tmp_str);
} else {
factId[i] = NA_STRING;
}
ns[i] = (char *) fact_node->ns->href;
}
xmlXPathFreeObject(fact_res);
return DataFrame::create(Named("elementId")=elementId,
Named("contextId")=contextId,
Named("unitId")=unitId,
Named("fact")=fact,
Named("factId")=factId,
Named("decimals")=decimals,
Named("scale")=scale,
Named("sign")=sign,
//.........这里部分代码省略.........
示例11: fixup_sub
double EqonomizeValueEdit::fixup_sub(QString &input, QStringList &errors, bool &calculated) const {
input = input.trimmed();
if(input.isEmpty()) {
return 0.0;
}
if(o_currency) {
input.remove(budget->monetary_group_separator);
if(!budget->monetary_negative_sign.isEmpty()) input.replace(budget->monetary_negative_sign, "-");
if(!budget->monetary_positive_sign.isEmpty()) input.replace(budget->monetary_positive_sign, "+");
} else {
input.remove(budget->group_separator);
if(!budget->negative_sign.isEmpty()) input.replace(budget->negative_sign, "-");
if(!budget->positive_sign.isEmpty()) input.replace(budget->positive_sign, "+");
}
input.replace(QLocale().negativeSign(), '-');
input.replace(QLocale().positiveSign(), '+');
int i = input.indexOf(')', 1);
if(i < 1) {
i = input.indexOf('(', 0);
if(i == 0) {
input.remove(0, 1);
return fixup_sub(input, errors, calculated);
} else if(i >= 0) {
input += ')';
i = input.length() - 1;
} else {
i = -1;
}
}
if(i >= 1) {
int i2 = input.lastIndexOf('(', i - 1);
if(i2 < 0 || i2 > i) {
if(i == input.length() - 1) {
input.chop(1);
return fixup_sub(input, errors, calculated);
}
input.prepend('(');
i++;
i2 = 0;
}
if(i2 == 0 && i == input.length() - 1) {
input.remove(0, 1);
input.chop(1);
return fixup_sub(input, errors, calculated);
}
if(i < input.length() - 1 && (input[i + 1].isNumber() || input[i + 1] == '(')) input.insert(i + 1, '*');
QString str = input.mid(i2 + 1, i - i2 - 1);
double v = fixup_sub(str, errors, calculated);
input.replace(i2, i - i2 + 1, o_currency ? o_currency->formatValue(0.5, decimals() + 2, false, false, true) : budget->formatValue(v, decimals() + 2));
if(i2 > 0 && (input[i2 - 1].isNumber() || input[i2 - 1] == ')')) input.insert(i2, '*');
calculated = true;
return fixup_sub(input, errors, calculated);
}
i = input.indexOf(QRegExp("[-+]"), 1);
if(i >= 1) {
QStringList terms = input.split(QRegExp("[-+]"));
i = 0;
double v = 0.0;
QList<bool> signs;
signs << true;
for(int terms_i = 0; terms_i < terms.size() - 1; terms_i++) {
i += terms[terms_i].length();
if(input[i] == '-') signs << false;
else signs << true;
i++;
}
for(int terms_i = 0; terms_i < terms.size() - 1; terms_i++) {
if(terms[terms_i].endsWith('*') || terms[terms_i].endsWith('/') || terms[terms_i].endsWith('^')) {
if(!signs[terms_i + 1]) terms[terms_i] += '-';
else terms[terms_i] += '+';
terms[terms_i] += terms[terms_i + 1];
signs.removeAt(terms_i + 1);
terms.removeAt(terms_i + 1);
terms_i--;
}
}
if(terms.size() > 1) {
for(int terms_i = 0; terms_i < terms.size(); terms_i++) {
if(terms[terms_i].isEmpty()) {
if(!signs[terms_i] && terms_i + 1 < terms.size()) {
signs[terms_i + 1] = !signs[terms_i + 1];
}
} else {
if(!signs[terms_i]) v -= fixup_sub(terms[terms_i], errors, calculated);
else v += fixup_sub(terms[terms_i], errors, calculated);
}
}
calculated = true;
return v;
}
}
if(input.indexOf("**") >= 0) input.replace("**", "^");
i = input.indexOf(QRegExp("[*/]"), 0);
if(i >= 0) {
QStringList terms = input.split(QRegExp("[*/]"));
QChar c = '*';
i = 0;
double v = 1.0;
for(int terms_i = 0; terms_i < terms.size(); terms_i++) {
if(terms[terms_i].isEmpty()) {
//.........这里部分代码省略.........
示例12: decimals
QString EqonomizeValueEdit::textFromValue(double value) const {
if(o_currency) return o_currency->formatValue(value, decimals(), true, false, true);
if(!s_suffix.isEmpty()) return s_prefix + budget->formatValue(value, decimals()) + QString(" ") + s_suffix;
return s_prefix + budget->formatValue(value, decimals());
}
示例13: palette
void BigNumDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
bool selected = option.state & QStyle::State_Selected;
bool mouseOver = option.state & QStyle::State_MouseOver;
QPalette palette(option.palette);
if (selected){
palette.setBrush(QPalette::Active, QPalette::Window, option.palette.highlight());
palette.setBrush(QPalette::Active, QPalette::WindowText, option.palette.highlightedText());
}
else if (mouseOver) {
palette.setBrush(QPalette::Active, QPalette::Window, option.palette.button());
palette.setBrush(QPalette::Active, QPalette::WindowText, option.palette.buttonText());
}
else{
palette.setBrush(QPalette::Active, QPalette::Window, option.palette.base());
palette.setBrush(QPalette::Active, QPalette::WindowText, option.palette.text());
}
QRect labelRect(option.rect.x(), option.rect.y(), option.rect.width(), option.rect.height());
if (m_adjustPadding)
labelRect.adjust(LabelPadding, LabelPadding, -(2 * LabelPadding), -(2 * LabelPadding));
m_label->setPalette(palette);
m_label->setFixedSize(qMax(0, labelRect.width()), labelRect.height());
if (index.model()->data(index, Qt::DisplayRole).isNull()) {
m_label->clear();
}
else {
m_label->setText(
(index.model()->data(index, CustomStyleRole).toString().contains("%1") ? index.model()->data(index, CustomStyleRole).toString() : QString("%1"))
.arg(prefix() + m_label->locale().toString(index.model()->data(index, Qt::DisplayRole).toDouble(), 'f', decimals()) + suffix())
);
}
if (index.model()->data(index, Qt::TextAlignmentRole).isNull())
m_label->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
else
m_label->setAlignment(Qt::Alignment(index.model()->data(index, Qt::TextAlignmentRole).toInt()));
QPixmap pixmap(m_label->size());
m_label->render(&pixmap);
painter->drawPixmap(labelRect, pixmap);
}
示例14: switch
/** Validate a string against current constraint */
bool TextFieldBody::validate(const QString &s, int line, int col) {
bool ok = false;
switch ((MidpConstraint)(constraints & MIDP_CONSTRAINT_MASK)) {
case MIDP_CONSTRAINT_NUMERIC:
{
QRegExp numbers("^-?[0-9]*$");
if (numbers.match(s) >= 0) {
if (length() == 0) {
ok = true;
} else if (s[0] == '-') { /* only allow one '-' & at beginning */
if (text()[0] != '-' && line == 0 && col == 0) {
ok = true;
}
} else { /* number(s) being entered */
if (text()[0] != '-') { /* positive integer */
ok = true;
} else { /* negative integer-allow digits only after '-' */
if (col > 0 || line > 0) {
ok = true;
}
}
}
}
}
break;
case MIDP_CONSTRAINT_DECIMAL:
{
QRegExp decimals("^-?[0-9]*[.]?[0-9]*$");
if (decimals.match(s) >= 0) {
if (length() == 0) {
ok = true;
} else {
if (s[0] == '-') { /* only allow one '-' & at beginning */
if (text()[0] != '-' && line == 0 && col == 0) {
ok = true;
}
} else { /* number(s) being entered */
if (text()[0] != '-') { /* positive decimal */
ok = true;
} else { /* negative decimal-allow digits only after '-' */
if (col > 0 || line > 0) {
ok = true;
}
}
}
/* Only allow one dot(.) */
if (ok) {
if (s.find('.') >= 0 && text().find('.') >= 0) {
ok = false;
}
}
}
}
}
break;
case MIDP_CONSTRAINT_PHONENUMBER:
{
/* the phone number has to accept the '+' at the start of the text, any digits, '#' and '*' */
QRegExp numbers("^[\053]?[0-9\\s\052\043]*$");
if (numbers.match(s) >= 0) {
ok = true;
}
}
break;
case MIDP_CONSTRAINT_ANY:
case MIDP_CONSTRAINT_EMAILADDR:
case MIDP_CONSTRAINT_URL:
ok = true;
break;
} /* end of switch(constraints) */
return ok;
}
示例15: qWarning
/*
* The code of the following function is taken from kdeui/knumvalidator.cpp
* and adjusted to always use the monetary symbols defined in the KDE System Settings
*/
QValidator::State kMyMoneyMoneyValidator::validate(QString & input, int & _p) const
{
QString s = input;
// TODO: port this to kf5
#if 0
KLocale * l = KLocale::global();
// ok, we have to re-format the number to have:
// 1. decimalSymbol == '.'
// 2. negativeSign == '-'
// 3. positiveSign == <empty>
// 4. thousandsSeparator() == <empty> (we don't check that there
// are exactly three decimals between each separator):
QString d = l->monetaryDecimalSymbol(),
n = l->negativeSign(),
p = l->positiveSign(),
t = l->monetaryThousandsSeparator();
// first, delete p's and t's:
if (!p.isEmpty())
for (int idx = s.indexOf(p) ; idx >= 0 ; idx = s.indexOf(p, idx))
s.remove(idx, p.length());
if (!t.isEmpty())
for (int idx = s.indexOf(t) ; idx >= 0 ; idx = s.indexOf(t, idx))
s.remove(idx, t.length());
// then, replace the d's and n's
if ((!n.isEmpty() && n.indexOf('.') != -1) ||
(!d.isEmpty() && d.indexOf('-') != -1)) {
// make sure we don't replace something twice:
qWarning() << "KDoubleValidator: decimal symbol contains '-' or "
"negative sign contains '.' -> improve algorithm" << endl;
return Invalid;
}
if (!d.isEmpty() && d != ".")
for (int idx = s.indexOf(d) ; idx >= 0 ; idx = s.indexOf(d, idx + 1))
s.replace(idx, d.length(), ".");
if (!n.isEmpty() && n != "-")
for (int idx = s.indexOf(n) ; idx >= 0 ; idx = s.indexOf(n, idx + 1))
s.replace(idx, n.length(), "-");
// Take care of monetary parens around the value if selected via
// the locale settings.
// If the lead-in or lead-out paren is present, remove it
// before passing the string to the QDoubleValidator
if (l->negativeMonetarySignPosition() == KLocale::ParensAround
|| l->positiveMonetarySignPosition() == KLocale::ParensAround) {
QRegExp regExp("^(\\()?([\\d-\\.]*)(\\))?$");
if (s.indexOf(regExp) != -1) {
s = regExp.cap(2);
}
}
// check for non numeric values (QDoubleValidator allows an 'e', we don't)
QRegExp nonNumeric("[^\\d-\\.]+");
if (s.indexOf(nonNumeric) != -1)
return Invalid;
// check for minus sign trailing the number
QRegExp trailingMinus("^([^-]*)\\w*-$");
if (s.indexOf(trailingMinus) != -1) {
s = QString("-%1").arg(trailingMinus.cap(1));
}
// check for the maximum allowed number of decimal places
int decPos = s.indexOf('.');
if (decPos != -1) {
if (decimals() == 0)
return Invalid;
if (((int)(s.length()) - decPos) > decimals())
return Invalid;
}
// If we have just a single minus sign, we are done
if (s == QString("-"))
return Acceptable;
QValidator::State rc = QDoubleValidator::validate(s, _p);
if (rc == Acceptable) {
// If the numeric value is acceptable, we check if the parens
// are ok. If only the lead-in is present, the return value
// is intermediate, if only the lead-out is present then it
// definitely is invalid. Nevertheless, we check for parens
// only, if the locale settings have it enabled.
if (l->negativeMonetarySignPosition() == KLocale::ParensAround
|| l->positiveMonetarySignPosition() == KLocale::ParensAround) {
int tmp = input.count('(') - input.count(')');
if (tmp > 0)
rc = Intermediate;
else if (tmp < 0)
rc = Invalid;
}
}
return rc;
//.........这里部分代码省略.........