I need to turn my string decimals into decimals to use with highcharts API.
I need to keep my trailing zeros so I have a string like this "0.00030900" and I turn it into the exact same thing without the quotes 0.00030900
I need it to stay exactly the same format so that way it looks correct when I return it to the screen.
I know 0.00030900 is the same as 0.000309 mathematically but visually they are not the same and I need them to look visually the same.
I've tried floatval() that strips it off my zeros. I've tried multiplying my strings by 1 and then using number_format() to add my zeros back but number_format turns my decimal back into a string.
You will need to use the highcharts formatter callback feature to achieve this since that format is nonstandard.
Note that is has nothing to do with php whatsoever. You just provide your values to highcharts as plain floating-point values and reformat them there for display as necesary (otherwise you'll either waste your time or run into problems in your graph, such as values not being ordered properly).
A few such properties in highcharts are :
http://api.highcharts.com/highcharts/xAxis.labels.formatter
http://api.highcharts.com/highcharts/legend.labelFormatter
http://api.highcharts.com/highcharts/tooltip.formatter
In any case, your callback function would have to get the floating-point number value, convert it to a string, and pad any number of zeroes to the right until it is the right length.
Related
I have a scenario where I need to parsefloat 19 digit string to number.
e.g. parseFloat("1000000000100000043") gives me 1000000000100000000
but the expected output required is 1000000000100000043
This is likely a precision overflow error.
The Number data type (but also int and float in other languages) have a finite number of bits available to represent a number. Typically around 15-16 decimal digits worth.
When length of original number in the string exceeds available precision, such number can no longer be represented by the target data type.
In this case the parseFloat function fails silently. If you want to catch this situation you need to add code to check incoming data or use another function, possibly a custom one.
Alternatively, you can convert the numeric value back to string and compare it with original to detect a discrepancy.
See also a question regarding double.Parse
You are running into how Javascript numbers are stored. See, e.g., here: https://www.w3schools.com/js/js_numbers.asp
You can use a library like decimal.js to work with large, exact numbers. These libraries store the number as string, but allow you to do mathematical operations.
I am trying to use highcharts to make graphs. I currently have a string which I am looking to convert to a JSON array which looks like the following:
[{"chart":{"type":"line","renderTo":"chart_0"},"title":{"text":"Daily Sales & Spend"},
"xAxis":{"categories":["08-03-2015","08-04-2015"]},
"yAxis":{"title":{"text":"Dollars"}},
"series":[{"name":"Spend","data":[73.84,73.75]},{"name":"Sales","data":[1020.90,4007.90]}]}]
I need the trailing zeros so that 1020.90 stays 1020.90, on conversion the data becomes the following after the first index:
{"chart":{"type":"line","renderTo":"chart_0"},
"title":{"text":"Daily Sales & Spend"},
"xAxis":{"categories":["08-03-2015","08-04-2015"]},
"yAxis":{"title":{"text":"Dollars"}},
"series":[{"name":"Spend","data"[73.84,73.75]},{"name":"Sales","data":[1020.9,4009.9]}]}
The 1020.90 converts to 1020.9. I think this is a behavior of the float, but is it possible to convert it to 1020.90 [for display purposes]? I need this for displaying the data using highcharts.
Prevent JSON.parse(data) from cutting off zero digit for String floats
This has nothing to do with JSON; this is about how numbers work in JavaScript
The 1020.90 converts to 1020.9
There is no "conversion" here. They're the same number.
I need this for displaying the data using highcharts
Then you need to pad the number with trailing zeros when you convert it to a string.
It is impossible to tell a float how many significant digits it has. You can only impose significant digits when you display the float as a string.
Im designing a conversion website where i perform calculations on inputted numbers and i need my input or textarea to receive and interpret numbers entered in different fashions
like:
Entry = 3,000,000.1111
Interpreted value = 3000000.1111
or
Entry = 3000000.1111
Interpreted value = 3000000.1111
and I want to include a second input for European decimal notation
(or if possible have the same input do both)
Entry = 3.000.000,1111 (comma acts a decimal, decimal as separator)
Interpreted value = 3000000.1111
I wonder how I could do this. I suspect from some of my research that I could use regex.
Also should i use an input or a textarea? I want to limit the size of the number to 40 places.
It seems the textarea Im currently using won't recognize any values after a comma when a comma is used. I realized this is due to parseFloat. So I need to remove the commas using .replace() before parsing. But what do I do in the instance of European notation where the comma IS the decimal point? I suspect I should use regex to identify if a number is in comma decimal notation or standard decimal point notation and then outline the appropriate replacement behavior based on that. Any ideas how to write regex to identify a number between .0000000001 and 1,000,000,000,000,000 by only the separator and decimal point? What about when the entry doesn't use either? 12000 for example. Any help with this would be appreciated. Using HTML5 and Javascript. I am not using a form and am new at this. This is my first web page so please be patient with my questions.
I was thinking about this:
input = //value from textarea as a string
if(/REGEX which determines that the structure of the number is N,NNN.NN/.test(input)){
input = input.replace(/\,/,""); //replace the commas with nothing
}
else if(/REGEX which determine that structure of the number is N.NNN,NN/.test(input){
input = input.replace(/\./,""); //replace the decimal point separators with nothing
input = input.replace(/\,/,".");//replace the comma decimal with a point decimal
}
else{
//input unchanged assuming is NNNN without decimal
}
number = parseFloat(input);
I want to keep the possibility open for them to enter large numbers and also to use numbers less than one to 10 decimal places. Thanks to those who contributed.
Best,RP
I believe this should handle everything:
^[1-9](?:\d{0,2}(?:([,.])\d{3})*|\d+)(?:(?!\1)[,.]\d+)?$
You're treading on complicated territory here. Also, the above RegEx does not allow for values less than "1".
Basically, the RegEx does the following:
Allows for no thousandths separators ("," or ".") but ensures if they are used that they occur in the correct places.
Allows for either "," or "." to be used as both thousandths/cents separators, but ensures that the cents separator is not the same as the thousandths separator.
Requires the string equivalent number to begin with any digit other than "0".
To implement this you could attach an event listener to your form element(s) and use JS to do a simple .test.
After reading further, I think I misinterpreted your goal originally. I assumed you simply wanted to validate these values with a RegEx. I also assumed you're trying to work with currency (ie. two decimal places). However, fret not! You can still utilize my original answer if you really want.
You mentioned input and textarea which are both form elements. You can attach a listener to these element(s) looking for the input, change, and/or keyup events. As a part of the callback you can run the .test method or some other functionality. Personally, I would rethink how you want to handle input. Also, what's your actual goal here? Do you really need to know the thousandths separator or keep track of it? Why not just disallow any characters other than the one decimal point/comma and digits?
Also, parsing numbers like .0000000001 as a float is a terrible idea. You will lose precision very quickly if you do any sort of calculations such as multiplication, division, power, etc. You're going to have to figure out a different method to do this like storing the number to the right separately and as integers instead then go from there.
I can help you if you describe what you're trying to do in better detail.
I have the problem that when i round a number to 2 decimals the parseFloat() function removes .00 from the number. I have tried
var num = parseFloat(Math.round(19 * 100) / 100).toFixed(2);
The return: num="19.00"
The return i need: num = 19.00
I know 19 = 19.00, but i am using a service that always require two decimals .00
The function returns a string with the right value. When i parse it to float the .00 is removed.
You cannot get 19.00 as float, only as string, because numbers always remove trailing zeros.
Maybe you can show us a bit more code to get an idea, there you need these trailing zeros?
Numbers do and can not hold information about their representation. They are only a numerical value.
When you display a number using window.alert, console.log or similar, you are not looking at a number, but at a string. Those display functions convert numbers to strings before displaying them. Number.toFixed also converts numbers into strings, with the difference being that it rounds them to two decimal places, so you end up with another representation of the same number.
What I am trying to say is that to display a number, you cannot get around converting it to a string. Whether you do it explicitly or the display function does it for you. When you send the number to the service that you are using, you are probably also sending a string (JSON, XML, etc. are always strings once you send them). If you need the value of the number for calculations, use it, then convert it in the end. No matter how, you have to do it in the end if you want those 0's at the end.
I am using jqplot, and it gets marks by default like this:
What should I do to get it without .0 at the end?
Since it was indeed the answer I'll post it as an actual one.
With stringFormat, you can format the string of the axis labels, and it uses sprintf notation, where %d is a signed integer. Since integers do not have decimals, it is probably what you want.