utils.stringReplace treats numbers in the wrong way

Hello,

this code shows the problem best

var aText = 'Here is the number: %%number%%';
var aNumber = 1;
var aResult = utils.stringReplace(aText, '%%number%%', aNumber);
// aResult = "Here is the number: 1.0"
var anotherResult = 'Here is the number: ' + aNumber;
// anotherResult = "Here is the number: 1"

If I “cast” the number myself by doing

var aResult = utils.stringReplace(aText, '%%number%%', aNumber + '');

it works as expected.

Can this be fixed?

Thanks
Patrick

The number of decimals of a number is 1 by default. You can use toFixed() to set the number of decimals:

utils.stringReplace(aText, '%%number%%', aNumber.toFixed(0));

I knew there was a reason, but in my eyes it is still a misbehaviour. I use generic code to replace %%field%% parameters in XML and rtf documents. Whenever I encounter a integer column, I have this problem. It is unexpected, because I ask this method to replace %%field%% by “1” and it replaces it by “1.0”. The problem is that I cannot determine the datatype of a column. So I can’t really take care of this myself. All I can do is create a calc that converts that integer to a text or do that workaround that I use know:

var aResult = utils.stringReplace(aText, ‘%%number%%’, aNumber + ‘’);

I think the actual value is 1.0, but it’s displayed as 1, therefor, you think it’s just 1 and the logic doesn’t work as you would have expected.

Can’t you determine the type of your variable “aNumber” with JavaScript? Dunno the correct syntax, but I thin it’s something like “typeof”.

Paul

I don’t think it’s a 1.0, because the value comes from an integer column. And yes, there is a typeOf, but that doesn’t always do what you want it to (especially with dates). Anyway, I think it is easier to do a instanceOf in Java (so in the code of utils…) than messing around with JavaScript.

Plus, if you look at

var anotherResult = 'Here is the number: ' + aNumber;
// anotherResult = "Here is the number: 1"

there is no problem. It’s an integer just as I expect it to be. My problem only occurs when I pass this through utils…

Ehum, that’s what I meant: instanceOf. Is that Java only? Or also available in JavaScript?

Anyway, if it doesn’t happen when you concatenate is directly, but it does happen through Utils, I guess it’s an issue in the Utils library.

Yes, it’s Java only. But even: why should I do all kinds of if(typeOf(someField))… if I want to replace one thing by another. I just want the replaced result to be an integer, and not a 1000.0.

…but it does happen through Utils, I guess it’s an issue in the Utils library.

That’s what this thread is about :wink:

fixed it (for 2.2.6 and 3.0)
I use the same fix, the JavaScript NumberToString method, in as many functions i could find

Thanks :-)