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
9if (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]
hmmmThis 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 copypaste 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