Logstash conversion from float to scientific - logstash

I have an element in a CSV file that has a value of 0.022222222. Naturally, this hits the float data type limitation. However, logstash is converting it to the scientific notation.
I want the value in Elasticsearch to be float and not string. How can I achieve that?
Logstash and ES 7.1.0
Thanks in advance

try the below code
mutate {
convert => {
"fieldname" => "float"
}
}

Related

How to change number format in Logstash

I have numbers like
1.553
23
2.383
26
566
When I parse this with logstash, I get string value of these.
When I try to use mutate filter plugin like below;
mutate {
convert => {
"number_field" => "integer"
}
}
It returns wrong.
I mean as an example,2.383 as input goes 2 as output.
My expections is below
1553
23
2383
566
How can i solve this ?
Thanks for answering
When mutate+convert is used to convert to integer it will truncate floats. You can use integer_eu to tell it that the a dot is used instead of a comma to separate thousands (e.g., "1.000" produces an integer with value of one thousand).

Convert String to Decimal Value Logstash Mutate

I have the following line:
012018121212XTED11 010FII TRXE CORCI R$ 000000000169000000000017450000000001690000000000172700000000017170000000001717000000000175000051000000000000002011000000000003474670000000000000009999123100000010000000000000BRXTEDCTF006143\r
So i create a filter to extract the price:
grok {
match => ["message", "(?<tipo_registro>.{2})(?<data_pregao>.{8})(?<codbdi>.{2})(?<codneg>.{12})(?<tpmerc>.{3})(?<nomres>.{12})(?<especi>.{10})(?<prazot>.{3})(?<modref>.{4})(?<preabe>.{13})(?<premax>.{13})(?<premin>.{13})(?<premed>.{13})(?<preult>.{13})(?<preofc>.{13})(?<preofv>.{13})"]
}
The price is field preabe, but i need add a dot before two digits to convert to float, so this is my price in string :
00001650
I need add "." , so i will have
000016.50
Now convert to float and i will have
16.50
Any tips ?
Sounds like a job for ruby. Here's some untested code:
filter {
ruby {
code => "event.set('preabe', event.get('preabe').to_i / 100)"
}
}

How to sum up all numbers in an array

I have an array which outputs the following:
charges = [5.00, 26.00, 8.00, 4.00, 4.00, -8.00, 54.00, 52.48]
When I try to perform a sum using this:
charges.sum()
It gives me:
5.0026.008.004.004.00-8.0054.0052.48
I am assuming I need to convert it from a string to a float so I did:
Float.valueOf((String) charges.sum())
and it gives me an error which states 'multiple points'.
My question is how do I add all of these figures up?
If your list is actually of strings, you can quickly do the conversion with sum()'s closure form.
charges.sum { it.toBigDecimal() }
It is unclear what your list has in it, it seems like the entries in your list are actually strings (since sum concatenates them), or something that prints a string value (has a toString method that returns a string showing the value so you think it is numeric even though it isn’t) . They are definitely not numeric, so convert each of them to a numeric type before summing:
charges.collect { new BigDecimal(it.toString()) }.sum()
(What your code was doing was concatenating the string values together, then converting that to a numeric type.)
You must delete the cast (String)
Float.valueOf(charges.sum())

NodeJS: Convert string X,XXX.XX to float

I'm bothering you because I'm looking to convert a string into a float with node.JS.
I have a string that comes back from a request and looks like:
x,xxx.xxxx (ex. 4,530.1200) and I'd like to convert it to a float: xxxx.xxxx (or 4530.1200).
I've tried to use:
parseFloat(str) but it returns 4. I also found Float.valueOf(str) on the web but I get a ReferenceError: Float is not defined error.
You can use string replace to solve this kind of problem.
str = '4,530.1200';
parseFloat(str.replace(/,/g, ''));
This will remove all the , from your string so that you can convert it to the float using parseFloat.
Notice the g flag in regex, it represents global, by using this you are specifying to remove all the matched regex.

NodeJS - Convert hexadecimal to float

That may sound like a weird struggle and actually easy to do, but I cannot find a working way to convert an hexidecimal in a string format into a float.
My exemple is for instance: 406ea716
If I convert it using one of the following website, I get 3.728948.
http://www.h-schmidt.net/FloatConverter/IEEE754.html
http://gregstoll.dyndns.org/~gregstoll/floattohex/
I tried every single piece of code I found on the internet, but it won't return the same result.
Does it exist a module in NodeJS to perform the same conversion? If not, what can I do?
Thank you for your help.
I had the same issue. try this.
Buffer('406ea716','hex').readFloatBE(0)
3.7289481163024902
No need for a module:
var hex = '406ea716';
// transform the hexadecimal representation in a proper js hexadecimal representation by prepending `0x` to the string
// parseInt() - because your example was an integer.
var num = parseInt( '0x' + '406ea716');
console.log( num );
Have you tried parseInt?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
$ node
> parseInt('406ea716', 16)
1080993558

Resources