# utils.stringReplace treats numbers in the wrong way

**URL:** <https://forum.servoy.com/t/utils-stringreplace-treats-numbers-in-the-wrong-way/6243>\
**Category:** Classic Servoy\
**Created:** [July 18, 2006, 2:54pm UTC](https://forum.servoy.com/t/utils-stringreplace-treats-numbers-in-the-wrong-way/6243 "2006-07-18T14:54:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [July 18, 2006, 2:54pm UTC](https://forum.servoy.com/t/utils-stringreplace-treats-numbers-in-the-wrong-way/6243/1 "2006-07-18T14:54:55Z")

</div>

Hello,

this code shows the problem best

```auto
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

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

```

it works as expected.

Can this be fixed?

Thanks  
Patrick

---

<div class="post-metadata">

**Author:** ![Joas](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/joas/32/5095_2.png) [@Joas](https://forum.servoy.com/u/Joas)\
**Post date:** [July 18, 2006, 3:47pm UTC](https://forum.servoy.com/t/utils-stringreplace-treats-numbers-in-the-wrong-way/6243/2 "2006-07-18T15:47:03Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [July 18, 2006, 5:07pm UTC](https://forum.servoy.com/t/utils-stringreplace-treats-numbers-in-the-wrong-way/6243/3 "2006-07-18T17:07:33Z")

</div>

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 + ‘’);

---

<div class="post-metadata">

**Author:** ![pbakker](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@pbakker](https://forum.servoy.com/u/pbakker)\
**Post date:** [July 19, 2006, 6:47am UTC](https://forum.servoy.com/t/utils-stringreplace-treats-numbers-in-the-wrong-way/6243/4 "2006-07-19T06:47:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [July 19, 2006, 6:53am UTC](https://forum.servoy.com/t/utils-stringreplace-treats-numbers-in-the-wrong-way/6243/5 "2006-07-19T06:53:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [July 19, 2006, 6:54am UTC](https://forum.servoy.com/t/utils-stringreplace-treats-numbers-in-the-wrong-way/6243/6 "2006-07-19T06:54:37Z")

</div>

Plus, if you look at

```auto
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…

---

<div class="post-metadata">

**Author:** ![pbakker](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@pbakker](https://forum.servoy.com/u/pbakker)\
**Post date:** [July 19, 2006, 7:00am UTC](https://forum.servoy.com/t/utils-stringreplace-treats-numbers-in-the-wrong-way/6243/7 "2006-07-19T07:00:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [July 19, 2006, 7:51am UTC](https://forum.servoy.com/t/utils-stringreplace-treats-numbers-in-the-wrong-way/6243/8 "2006-07-19T07:51:31Z")

</div>

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:]( "Wink")

---

<div class="post-metadata">

**Author:** ![jcompagner](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/jcompagner/32/4889_2.png) [@jcompagner](https://forum.servoy.com/u/jcompagner)\
**Post date:** [July 24, 2006, 12:50pm UTC](https://forum.servoy.com/t/utils-stringreplace-treats-numbers-in-the-wrong-way/6243/9 "2006-07-24T12:50:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [July 24, 2006, 12:51pm UTC](https://forum.servoy.com/t/utils-stringreplace-treats-numbers-in-the-wrong-way/6243/10 "2006-07-24T12:51:38Z")

</div>

Thanks ![:-)]( "Smile")
