jqPlot ticks without decimals - javascript

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.

Related

Maintain decimal points with 0 in JSON object [duplicate]

If you do this...
var parsed = JSON.parse('{"myNum":0.0}') ;
Then when you look at parsed.myNum, you just get 0. (Fair enough.)
If you do parsed.myNum.toString(), you get "0".
Basically, I'm looking for a way to turn this into the string "0.0".
Obviously, in real life I don't control the JSON input, I get the JSON from a web service... I want to parse the JSON using the browser's JSON parser, and to be able to recognise the difference between the number values 0 and 0.0.
Is there any way to do this, short of manually reading the JSON string? That's not really possible in this case, I need to use the browser's native JSON parser, for speed. (This is for a Chrome extension by the way; no need to worry about it working in other browsers.)
There's no way to get the number of digits from JSON.parse or eval. Even if IBM's decimal proposal had been adopted by the EcmaScript committee, the number is still going to be parsed to an IEEE 754 float.
Take a look a http://code.google.com/p/json-sans-eval/source/browse/trunk/src/json_sans_eval.js for a simple JSON parser that you can modify to keep precision info.
If 0.0 is not enclosed in quotes in your JSON (i.e. it's a number and not a string), then there's no way to distinguish it from 0, unless you write your own JSON parser.
... parsed.myNum.toFixed( 1 ) ...
where 1 is number of decimal places
Edit: parsed.myNum is number, parsed.myNym.toFixed( 1 ) would be string
Edit2: in this case you need to pass value as string {"myNum":'0.0'} and parsed when calculations is needed or detect decimal separator, parsed number, and use decimal separator position when string is needed
It shouldn't matter, 00000.00000 is 0 if you try JSON.parse('{"myNum":0.00001}') you'll see a value of { myNum=0.0001 } If you really need it to hold the decimal you'll need to keep it a string JSON.parse('{"myNum":"0.0"}')

Php turn decimal string into decimal, but keep trailing zeros

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.

Prevent JSON.parse(data) from cutting off zero digit for String floats

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.

JavaScript parseFloat in Different Cultures

I have a question about the default behavior of JavaScript's parseFloat function in different parts of the world.
In the US, if you call parseFloat on a string "123.34", you'd get a floating point number 123.34.
If I'm developing code in say Sweden or Brazil and they use a comma instead of a period as the decimal separator, does the parseFloat function expect "123,34" or "123.34".
Please note that I'm not asking how to parse a different culture's number format in the US. I'm asking does parseFloat in Sweden or Brazil behave the same way it does inside the US, or does it expect a number in its local format? Or to better think about this, does a developer in Brazil/Sweden have to convert strings to English format before it can use parseFloat after extracting text from a text box?
Please let me know if this doesn't make sense.
parseFloat doesn't use your locale's definition, but the definition of a decimal literal.
It only parses . not ,
I'm brazilian and I have to replace comma with dot before parsing decimal numbers.
parseFloat specification
No, parseFloat is specified to parse DecimalLiterals, which use the dot as decimal separator. It does not depend on the current environment's locale settings.
It’s not just Sweden/Brazil. F.ex in US they often add commas in large numbers, like $5,762,325.25.
The parseFloat function essentially deals with decimals, not locale strings.
In general, JavaScript can sometimes convert generic strings/numbers/dates to locale-friendly formats, but not the other way around.
Complementing the answer given by FrancescoMM, you must use regex for really big numbers. String.replace using string as a parameter will replace only the first occurrence. So 999.999.999,99 becomes 999999.999.99
stringNum.replace(/\./g, "").replace(/\,/g, ".")
Source:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
If you are sure it is in Brasilian format, just convert the number to US format before parsing.
function parseItalianNumber(stringNum) {
return parseFloat(stringNum.replaceAll(".","").replaceAll(",","."));
}
in Italy we also use . as a thousands separator. This removes any thousands separators, just in case (you do not want many dots around), and then converts the comma to a dot, before calling parseFloat.

How to prevent removing decimal point when parsing JSON?

If you do this...
var parsed = JSON.parse('{"myNum":0.0}') ;
Then when you look at parsed.myNum, you just get 0. (Fair enough.)
If you do parsed.myNum.toString(), you get "0".
Basically, I'm looking for a way to turn this into the string "0.0".
Obviously, in real life I don't control the JSON input, I get the JSON from a web service... I want to parse the JSON using the browser's JSON parser, and to be able to recognise the difference between the number values 0 and 0.0.
Is there any way to do this, short of manually reading the JSON string? That's not really possible in this case, I need to use the browser's native JSON parser, for speed. (This is for a Chrome extension by the way; no need to worry about it working in other browsers.)
There's no way to get the number of digits from JSON.parse or eval. Even if IBM's decimal proposal had been adopted by the EcmaScript committee, the number is still going to be parsed to an IEEE 754 float.
Take a look a http://code.google.com/p/json-sans-eval/source/browse/trunk/src/json_sans_eval.js for a simple JSON parser that you can modify to keep precision info.
If 0.0 is not enclosed in quotes in your JSON (i.e. it's a number and not a string), then there's no way to distinguish it from 0, unless you write your own JSON parser.
... parsed.myNum.toFixed( 1 ) ...
where 1 is number of decimal places
Edit: parsed.myNum is number, parsed.myNym.toFixed( 1 ) would be string
Edit2: in this case you need to pass value as string {"myNum":'0.0'} and parsed when calculations is needed or detect decimal separator, parsed number, and use decimal separator position when string is needed
It shouldn't matter, 00000.00000 is 0 if you try JSON.parse('{"myNum":0.00001}') you'll see a value of { myNum=0.0001 } If you really need it to hold the decimal you'll need to keep it a string JSON.parse('{"myNum":"0.0"}')

Categories

Resources