# Arithmetic with string

**URL:** https://discuss.elastic.co/t/arithmetic-with-string/52131
**Category:** Logstash
**Created:** [June 7, 2016, 10:19pm UTC](https://discuss.elastic.co/t/arithmetic-with-string/52131 "2016-06-07T22:19:57Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![stecino](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@stecino](https://discuss.elastic.co/u/stecino)
#### Post date: [June 7, 2016, 10:19pm UTC](https://discuss.elastic.co/t/arithmetic-with-string/52131/1 "2016-06-07T22:19:57Z")

</div>

Hello,

I have a string "5.34" and I am trying to multiply it by number, say 1000.  
Can I do it with gsub or ruby plugin. I have read that ruby plugin is resource intensive, so i would prefer to do it with gsub if possible.

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [June 10, 2016, 5:40am UTC](https://discuss.elastic.co/t/arithmetic-with-string/52131/2 "2016-06-10T05:40:04Z")

</div>

Don't assume that regular expression substitutions are slower than ruby filters. Measure!

Do your strings always have two decimals?

---

<div class="post-metadata">

### Author: ![stecino](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@stecino](https://discuss.elastic.co/u/stecino)
#### Post date: [June 10, 2016, 7:06pm UTC](https://discuss.elastic.co/t/arithmetic-with-string/52131/3 "2016-06-10T19:06:40Z")

</div>

I am not sure, but from the rounding, most likely it will have two decimal points.

Here is what I have

input { stdin { codec =\> json } }  
output { stdout { codec =\> rubydebug { metadata =\> true } } }  
filter {  
mutate {  
add\_field =\> { "Test1"=\> "%{Test}" }  
convert =\> { "Test1" =\> "float"}  
}

ruby {code =\> "event['foo'] = event['Test1'] \* 1000"}  
}

I ran

echo '{"Test":"5.34"}' | ../../bin/logstash -f ../test.config

Here is the snippet from resulting JSON

```
     "Test1" => "5.34",
       "foo" => "5.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.34"

```

}

Shouldn't it multiply instead?

I can gsub . with "" but I need to count the number of places I need to move the decimal point.

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [June 10, 2016, 7:09pm UTC](https://discuss.elastic.co/t/arithmetic-with-string/52131/4 "2016-06-10T19:09:53Z")

</div>

The JSON you passed in has the value represented as a string, which would explain why the calculation failed and the string instead got concatenated 1000 times.

---

<div class="post-metadata">

### Author: ![stecino](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@stecino](https://discuss.elastic.co/u/stecino)
#### Post date: [June 10, 2016, 7:25pm UTC](https://discuss.elastic.co/t/arithmetic-with-string/52131/5 "2016-06-10T19:25:03Z")

</div>

It's converted into float

---

<div class="post-metadata">

### Author: ![stecino](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@stecino](https://discuss.elastic.co/u/stecino)
#### Post date: [June 10, 2016, 7:44pm UTC](https://discuss.elastic.co/t/arithmetic-with-string/52131/6 "2016-06-10T19:44:09Z")

</div>

Looks like adding the field and doing a convert in single mutate is the problem. I found on this [Convert String to float failed](https://discuss.elastic.co/t/convert-string-to-float-failed/32232)

where someone was having the same type of issue. Ended up breaking up into to mutates

mutate { add\_field =\> { "Test1"=\> "%{Test}" }}  
mutate { convert =\> {"Test1"=\> "float"} }

This should be fixed.

So the resulting calculation now is as expected

```
     "Test1" => 5.34,
       "foo" => 5340.0

```

}

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 6, 2017, 4:53am UTC](https://discuss.elastic.co/t/arithmetic-with-string/52131/7 "2017-07-06T04:53:32Z")

</div>


