Commit b8e6f151 authored by Michael Pyne's avatar Michael Pyne
Browse files

Try to support the locale's current decimal separator in abakus instead of always using

US Decimal.

svn path=/trunk/playground/utils/abakus/; revision=460128
parent 3ea7ddec
......@@ -29,7 +29,7 @@
#include "mainwindow.h"
const char *const version = "0.90";
const char *const version = "0.91-rc1";
int main(int argc, char **argv)
{
......
......@@ -75,7 +75,7 @@ HEX [0-9A-Fa-f]+
* digits, such as .32, -234.45, .0, etc. Numbers are only read in the BEGIN
* state.
*/
{DIGITS}*(\.{DIGITS}+)(e[-+]?{DIGITS}+)? {
{DIGITS}*([\.,]{DIGITS}+)(e[-+]?{DIGITS}+)? {
yyCurTokenPos += yyThisTokenLength;
yyThisTokenLength = yyleng;
return NUM;
......@@ -91,7 +91,7 @@ HEX [0-9A-Fa-f]+
/* Read numbers with at least the integral part, such as +4234, -34e8, etc.
* Numbers are only read in the BEGIN state.
*/
{DIGITS}+(\.{DIGITS}*)?(e[-+]?{DIGITS}+)? {
{DIGITS}+([\.,]{DIGITS}*)?(e[-+]?{DIGITS}+)? {
yyCurTokenPos += yyThisTokenLength;
yyThisTokenLength = yyleng;
return NUM;
......
......@@ -21,6 +21,8 @@
#include "hmath.h"
#include <kdebug.h>
#include <kglobal.h>
#include <klocale.h>
Abakus::TrigMode Abakus::m_trigMode = Abakus::Degrees;
int Abakus::m_prec = -1;
......@@ -36,6 +38,7 @@ QString convertToString(const mpfr_ptr &number)
QRegExp zeroKiller ("0*$");
mp_exp_t exp;
int desiredPrecision = Abakus::m_prec;
QString decimalSymbol = KGlobal::locale()->decimalSymbol();
if(desiredPrecision < 0)
desiredPrecision = 8;
......@@ -77,7 +80,7 @@ QString convertToString(const mpfr_ptr &number)
r.append(QString("e%1").arg(exp - 1));
return sign + l + "." + r;
return sign + l + decimalSymbol + r;
}
else
{
......@@ -131,7 +134,7 @@ QString convertToString(const mpfr_ptr &number)
if(r.isEmpty())
return sign + l;
return sign + l + "." + r;
return sign + l + decimalSymbol + r;
}
} // namespace Abakus
......@@ -164,6 +167,36 @@ const Abakus::number_t::value_type Abakus::number_t::E = setupExponential();
#else
// Converts hmath number to a string.
namespace Abakus
{
QString convertToString(const HNumber &num)
{
QString str = HMath::formatGenString(num, m_prec);
QString decimalSymbol = KGlobal::locale()->decimalSymbol();
str.replace('.', decimalSymbol);
QStringList parts = QStringList::split("e", str);
QRegExp zeroKiller("(" + QRegExp::escape(decimalSymbol) +
"\\d*[1-9])0*$"); // Remove trailing zeroes.
QRegExp zeroKiller2("(" + QRegExp::escape(decimalSymbol) + ")0*$");
str = parts[0];
str.replace(zeroKiller, "\\1");
str.replace(zeroKiller2, "\\1");
if(str.endsWith(decimalSymbol))
str.truncate(str.length() - 1); // Remove trailing period.
if(parts.count() > 1 && parts[1] != "0")
str += QString("e%1").arg(parts[1]);
return str;
}
} // namespace Abakus.
const Abakus::number_t::value_type Abakus::number_t::PI = HMath::pi();
const Abakus::number_t::value_type Abakus::number_t::E = HMath::exp(1);
......
......@@ -506,6 +506,9 @@ inline number<mpfr_ptr> operator/(const number<mpfr_ptr> &l, const number<mpfr_p
#else
// Defined in numerictypes.cpp for ease of reimplementation.
QString convertToString(const HNumber &num);
/**
* Specialization for internal HMath library, used if MPFR isn't usable.
*
......@@ -663,21 +666,7 @@ public:
QString toString() const
{
QString str = HMath::formatGenString(m_t, m_prec);
QStringList parts = QStringList::split("e", str);
QRegExp zeroKiller("(\\.\\d*[1-9])0*$"); // Remove trailing zeroes.
QRegExp zeroKiller2("(\\.)0*$");
str = parts[0];
str.replace(zeroKiller, "\\1");
str.replace(zeroKiller2, "\\1");
if(str.endsWith("."))
str.truncate(str.length() - 1); // Remove trailing period.
if(parts.count() > 1 && parts[1] != "0")
str += QString("e%1").arg(parts[1]);
return str;
return convertToString(m_t);
}
static number<HNumber> nan()
......
......@@ -21,6 +21,7 @@
/* Add necessary includes here. */
#include <kdebug.h>
#include <klocale.h>
#include <kglobal.h>
#include <stdio.h>
#include <stdlib.h>
......@@ -314,6 +315,16 @@ TERM: VALUE { $$ = $1; }
VALUE: NUMBER { $$ = $1; }
NUMBER: NUM {
KLocale *locale = KGlobal::locale();
QChar decimal = locale->decimalSymbol()[0];
// Replace current decimal separator with US Decimal separator to be
// evil.
unsigned len = strlen(yytext);
for(unsigned i = 0; i < len; ++i)
if(yytext[i] == decimal)
yytext[i] = '.';
Abakus::number_t value(yytext);
$$ = new NumericValue(value);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment