Okay, let's make it a little less cheesy. Add a line to localprefs.js that
defines a decimal separator character; call it calculatorDecimal:
<localprefs.js>
/* Calculator decimal separator character.
* Value can be almost any character not used for computation.
* Default value is '.' (period), if no value is set
*/
calculatorDecimal = ",";
In calculate.js, add a little more code to check for variable
calculatorDecimal and use it:
<calculate.js>
9 if (typeof calculatorDecimal != "undefined" && calculatorDecimal !=
"") {
10 var re = new RegExp("\\" + calculatorDecimal, "g");
11 expr = expr.replace(re, ".");
12 }
Lines 24 & 25 have been inserted before original line 24 (now 26).
24 if (typeof calculatorDecimal != "undefined" &&
calculatorDecimal != "")
25 answer = answer.toString().replace(/\./g,
calculatorDecimal);
26 setSearchWindowText(answer, true);
Now, we can use comma for a decimal separator, and the answer will use the
comma, too. I included a sample localprefs.js and a modified calculate.js
file at the bottom of this message. Of course, we still have the same
problem with updates: our new calculate.js will be overwritten. Moreover,
this fix shouldn't be necessary in the first place: the JavaScript math
functions should be sufficiently region-aware to format numbers correctly.
It's possible that we could use the javascript function
navigator.systemLanguage to make a best guess about how to format numbers.
I'll submit a feature request to include this particular change, and perhaps
another feature request to support regional settings in general.
Regards,
Gregory Krohne
> From: "Monty Scroggins" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Subject: RE: [DQSD-Users] Calculator problem: comma vs. period
> Date: Thu, 30 Jan 2003 10:02:28 -0600
> Reply-To: [EMAIL PROTECTED]
>
> hmmm This is cheesy workaround for this problem, but you
> could maybe add
> this line - var expr =3D expr.replace(/\,/g, "."); at line 9
> of calculat= e.js. This replaces any commas with decimals in
> the math expression..
>
> Of course every toolbar upgrade will overwrite calculate.js,
> so you will need to make this change again if you upgrade....
>
> Monty
>
>
> >
> > This is a real problem, becouse almost everywhere else it's a comma,
> > like 234,5+15 which gives weird results like 20??
> >
> > Is this a localized problem, becouse I live in Finland and here the
> > decimal-separator is always a comma?
> >
> > If I copy&paste some numbers, I have to change the commas to periods
> > for the calculator to work.
> > Is there any solution that could fix the problem?
> >
> > Best regards,
> > Janne Riihim=E4ki
/* localprefs.js
*
* Add preferences here to keep them from being overwritten on upgrades.
*
* Use preferences.js as a guide, copying settings from that
* file to this file, and then modifying them to suit your
* personal taste.
*/
/* Calculator decimal separator character.
* Value can be almost any character not used for computation.
* Default value is '.' (period), if no value is set
*/
calculatorDecimal = ",";
/* calculate.js
* offline calculator
*/
function calculate(expr)
{
if (expr.match(/=ERR$/))
return;
try
{
if (typeof calculatorDecimal != "undefined" && calculatorDecimal != "")
{
var re = new RegExp("\\" + calculatorDecimal, "g");
expr = expr.replace(re, ".");
}
with(Math)
{
var answer = eval(expr);
if (typeof(answer) == "number")
{
// for the sake of pretty decimal numbers,
// round numbers that are very close to a ten-millionth
if (abs(answer) >= 0.001 &&
abs(round(answer * 1e+7) - answer * 1e+7) < 0.001)
answer = round(answer * 1e+7)/1e+7;
}
if (typeof calculatorDecimal != "undefined" && calculatorDecimal
!= "")
answer = answer.toString().replace(/\./g,
calculatorDecimal);
setSearchWindowText(answer, true);
}
}
catch (exception)
{
setSearchWindowText(expr + "=ERR=", true);
}
savevars();
}
// based log functions
function log10(n) { return ln(n)/ln10; }
function log2(n) { return ln(n)/ln2; }
// hex conversion for use in calculator
function hex(i)
{
hexChars = "0123456789abcdef";
var h = "";
var n = 256;
while (i < 0)
{
if (i + n / 16 > 0) { i += n; break; }
n = n * n;
}
while (i > 0)
{
h = hexChars.charAt(i % 16) + h;
i = Math.floor(i / 16);
}
if (h == "")
h = "0";
return "0x" + h;
}
// octal conversion for use in calculator
function oct(i)
{
octChars = "01234567";
var h = "";
var n = 512;
while (i < 0)
{
if (i + n / 8 > 0) { i += n; break; }
n = n * n;
}
while (i > 0)
{
h = octChars.charAt(i % 8) + h;
i = Math.floor(i / 8);
}
if (h == "")
h = "0";
return "0" + h;
}
// binary conversion for use in calculator
function bin(i)
{
binChars = "01";
var h = "";
var n = 16;
while (i < 0)
{
if (i + n / 2 > 0) { i += n; break; }
n = n * n;
}
while (i > 0)
{
h = binChars.charAt(i % 2) + h;
i = Math.floor(i / 2);
}
if (h == "")
h = "0";
return h;
}
// detect numeric math expressions to execute right away
function mathexp(t)
{
// chop out variables
var noconst =
t.replace(/\b(PI|E|LOG2E|LOG10E|LN2|LN10|SQRT2|SQRT1_2|pi)\b/g, "_");
// experiment: also allow "longvarname="
var noconst = noconst.replace(/^[a-zA-Z_]+\s*=/, "a=");
// it's only an expression if it ends with a digit, paren, or variable
if (!noconst.match(/\b[a-z]$|[_\d\)]$/)) return false;
// chop out function names
var nofunc =
noconst.replace(/\b(ln|log10|log2|hex|oct|bin|abs|acos|asin|atan|atan2|ceil|
cos|exp|floor|log|max|min|pow|random|round|sin|sqrt|tan)\(/g, "(");
// it's only an expression if it begins with a digit, paren, or variable -
or a minus
if (!nofunc.match(/^[a-z_]\b|^[x\d\-\.\(]/)) return false;
// detect numbers
var nonum = nofunc.replace(/\b(\d+(\.\d*)?)([eE][+-]?\d+)?\b/g, "1");
var nonum = nonum.replace(/(\.\d+)([eE][+-]?\d+)?\b/g, "1");
// remove spsaces
var nospace = nonum.replace(/\s/g, "");
// only operators and recognized things are allowed
if (!nospace.match(/^[1_a-z\*\-\+\/\(\),=]+$/)) return false;
// constants cannot touch
if (nospace.match(/[1_a-z\)][1_a-z\(]/)) return false;
// operators cannot touch
if (nospace.match(/[\*\-\+\/][\*\+\/=]/)) return false;
// parens must match
var paren = 0;
var eq = 0;
var i;
for (i = nospace.length - 1; i >= 0; i--)
{
var ch = nospace.charAt(i);
if (eq == 1)
{
if (ch < 'a' || ch > 'z') return false;
}
else
{
if (ch >= 'a' && ch <= 'z' && eval("typeof " + ch) == "undefined")
return false;
}
if (ch == '=') { eq++; }
if (ch == ')') { paren++; }
if (ch == '(') { paren--; }
if (paren < 0) return false;
if (eq > 1) return false;
}
// looks like an expression: we can calc it
calculate(t);
return true;
}
function savevars()
{
var savetext = "";
for (var ii = 'a'.charCodeAt(0); ii <= 'z'.charCodeAt(0); ii++)
{
var varname = String.fromCharCode(ii);
var vartype = eval("typeof " + varname);
if (vartype == "number" || vartype == "string")
{
savetext += varname + "\t" + vartype + "\t" + escape(eval(varname)) +
"\r\n";
}
}
for (var ii = 'A'.charCodeAt(0); ii <= 'Z'.charCodeAt(0); ii++)
{
var varname = String.fromCharCode(ii);
var vartype = eval("typeof " + varname);
if (vartype == "number" || vartype == "string")
{
savetext += varname + "\t" + vartype + "\t" + escape(eval(varname)) +
"\r\n";
}
}
writeFile("calcmem", savetext);
}
function loadvars()
{
var values = readTabDelimitedFile("calcmem")
for (var ii = 0; ii < values.length; ii++)
{
var value = values[ii];
if (value.length == 3)
{
if (value[1] == "number")
{
eval(value[0] + " = " + unescape(value[2]));
}
else if (value[1] == "string")
{
var strval = unescape(value[2]);
eval(value[0] + " = strval");
}
}
}
}
loadvars();
// constants for use in calculator
pi = Math.PI;
ln10 = Math.LN10;
ln2 = Math.LN2;
log10e = Math.LOG10E;
log2e = Math.LOG2E;
sqrt1_2 = Math.SQRT1_2;
sqrt2 = Math.SQRT2;
ln = Math.log;
e = Math.exp(1);
-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
_______________________________________________
To unsubscribe visit:
https://lists.sourceforge.net/lists/listinfo/dqsd-users
[EMAIL PROTECTED]
http://sourceforge.net/mailarchive/forum.php?forum_id=8601