# Painless to convert Kbps to Mbps

**URL:** <https://discuss.elastic.co/t/painless-to-convert-kbps-to-mbps/263944>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [February 11, 2021, 3:42am UTC](https://discuss.elastic.co/t/painless-to-convert-kbps-to-mbps/263944 "2021-02-11T03:42:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohanss08](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohanss08/32/82297_2.png) [@mohanss08](https://discuss.elastic.co/u/mohanss08)\
**Post date:** [February 11, 2021, 3:42am UTC](https://discuss.elastic.co/t/painless-to-convert-kbps-to-mbps/263944/1 "2021-02-11T03:42:12Z")

</div>

In kibana i have field in `kilobit/second` format, But to calculate the average and to visualize i would need to convert data into `megabit/second` .

![k18mK](https://us1.discourse-cdn.com/elastic/original/3X/3/9/397f75ffbd4f64fb048ce2ccdf64e80003a34c49.png)

```auto
String speed = doc['speed_download.keyword'].value;
long size = Long.parseLong(speed);
long k = (size/256.0);
long m = (size/1024.0);
long g = (size/1048576.0);
long t = (size/1073741824.0);

return size;

```

Tried with above configuration and code, but it is not working. I am new to ELK, So can someone help to achieve this using painless(scripted filed) in kibana? it would be great helpful.

---

<div class="post-metadata">

**Author:** ![ahmadimt07](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmadimt07/32/10031_2.png) [@ahmadimt07](https://discuss.elastic.co/u/ahmadimt07)\
**Post date:** [February 11, 2021, 5:04am UTC](https://discuss.elastic.co/t/painless-to-convert-kbps-to-mbps/263944/2 "2021-02-11T05:04:21Z")

</div>

The code will give `class_cast_exception`. please check that.

---

<div class="post-metadata">

**Author:** ![mohanss08](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohanss08/32/82297_2.png) [@mohanss08](https://discuss.elastic.co/u/mohanss08)\
**Post date:** [February 11, 2021, 5:21am UTC](https://discuss.elastic.co/t/painless-to-convert-kbps-to-mbps/263944/3 "2021-02-11T05:21:13Z")

</div>

Currently i have `doc['speed_download'].value / 1024` and preview result of this code shows below.

```auto
[
 {
  "_id": "3929212e-4aea-4100-acc1-95388f801452\n",
  "speed_download": 276.81662055567693,
  "new_speed_download": [
   0.27032873034477234
  ]
 },
 {
  "_id": "ace7c3c3-4473-42e9-bbb5-5a144811d357\n",
  "speed_download": 529.0886454264323,
  "new_speed_download": [
   0.5166881084442139
  ]
 },

```

Discover result for the scripted field shown as below.

```
0.7B
0B
0B
0.1B
1.2B

```

But i need to display in `KB` & `MB` instead of `B`

---

<div class="post-metadata">

**Author:** ![mohanss08](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohanss08/32/82297_2.png) [@mohanss08](https://discuss.elastic.co/u/mohanss08)\
**Post date:** [February 24, 2021, 5:31pm UTC](https://discuss.elastic.co/t/painless-to-convert-kbps-to-mbps/263944/4 "2021-02-24T17:31:11Z")

</div>

Any help would be highly appreciated.

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [February 24, 2021, 5:49pm UTC](https://discuss.elastic.co/t/painless-to-convert-kbps-to-mbps/263944/5 "2021-02-24T17:49:34Z")

</div>

The below seems to work for me. My data set doesn't have a number big enough for MB but I don't see why this wouldn't work.

**Scripted Field**

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/1/6/167cedfaeca42001cc91ed5d0d2a0227534499e1.png)

**Results**

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/7/07b97e2d5bc9a134b1900804aac7e79e233bb6ee.png)

---

<div class="post-metadata">

**Author:** ![mohanss08](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohanss08/32/82297_2.png) [@mohanss08](https://discuss.elastic.co/u/mohanss08)\
**Post date:** [February 25, 2021, 3:05am UTC](https://discuss.elastic.co/t/painless-to-convert-kbps-to-mbps/263944/6 "2021-02-25T03:05:39Z")

</div>

@aaron-nimocks. I am trying to visualize the average speed in [Day/Weekly/Monthly] basics using Line chart in that case it doesn't show the data in MB. it shows only in KBs. Ideally for the [Day/Weekly/Monthly] it need to show MBs.

My Line Chart visualization settings as follows.

```
Metrics
Y-Axis
Aggregation - Average
Field - New-Bytes

Buckets
X-Axis
Aggregation - Date Histogram
Field - Start_time
Minimum interval - Week
```

---

<div class="post-metadata">

**Author:** ![mohanss08](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohanss08/32/82297_2.png) [@mohanss08](https://discuss.elastic.co/u/mohanss08)\
**Post date:** [February 25, 2021, 7:43am UTC](https://discuss.elastic.co/t/painless-to-convert-kbps-to-mbps/263944/7 "2021-02-25T07:43:52Z")

</div>

@aaron-nimocks, Thanks a lot for your support. with this i can able to view the speed in Bytes/KBs/MBs.

`return doc['speed_download'].value*1024;`

---

<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:** [March 25, 2021, 7:44am UTC](https://discuss.elastic.co/t/painless-to-convert-kbps-to-mbps/263944/8 "2021-03-25T07:44:43Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
