1
var a1=$("#orderprogress").val().toFixed(2);//a1=50
var a2=$("#poprogress").val().toFixed(2); //a2=70

if i want to create a array like this how would i do it?

graphData = new Array(
             [a1 value,'#222222'],//[50,'#222222']
             [a2 value ,'#7D252B']//[70,'#222222']
        );
1
  • 1
    I think this is an okay question. The user has an issue, has tried something, and has shown code. It might be a trivial fix, but it's still an okay question. Commented Apr 12, 2013 at 19:33

4 Answers 4

4

Try using the following code:

var a1 = +(+$("#orderprogress").val()).toFixed(2);
var a2 = +(+$("#poprogress").val()).toFixed(2);

graphData = [
    [a1, '#222222'],//[50,'#222222']
    [a2, '#7D252B']//[70,'#222222']
];

DEMO: http://jsfiddle.net/ERccS/4/

This will take the textbox value (a string), convert it to a number, call toFixed(2) on it, then convert it back to a number.

Unfortunately (if you care), "50" will be displayed as 50 (this happens with trailing 0s). If you always need 2 decimal places no matter what, take off the first + I have in my code - they will be kept as strings and always have 2 decimal places.

I'm not exactly sure what you're looking to do with the toFixed. The .val() method always returns a string. toFixed isn't a String method - it's a Number method. And its result is the original Number rounded to a certain number of decimal places (what you pass to the method). In your case, it's 2.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm fetching data from server and setting the decimal precision to 2 . My string value is 50.0123123 so I'm reducing it to 50.01
@user1507789 Sounds good, then yeah, you'll have to use my code above for setting a1 and a2
.toFixed(2) returns a string. check jsfiddle.net/ERccS/1. you have to cast again to get the result as number. By the way you had given an explanation toFixed isn't a String method - it's a Number method
@RajaprabhuAravindasamy Thanks for pointing that out. I forgot that the result was expected to be a number. And I'm not sure what you meant by "By the way you had given an explanation toFixed isn't a String method - it's a Number method"
2
graphData = [[a1, '#222222'], [a2, '#7D252B']];

Comments

2

Either

var graphData = new Array(new Array(a1,'#222222'),new Array(a2 ,'#7D252B'));

OR

var graphData = [[a1,'#222222'],[a2,'#7D252B']];

Comments

0

try this,

var xArray=[];

var a1=$("#orderprogress").val().toFixed(2);//a1=50
var a2=$("#poprogress").val().toFixed(2); //a2=70

InsertIntoArray(a1,'#fff')
InsertIntoArray(a2,'#fff')

function InsertIntoArray(x,y)
{
var yArray=[x,y];

xArray.push(yArray);

}

or simply,

   var xArray=[];

    var a1=$("#orderprogress").val().toFixed(2);//a1=50
    var a2=$("#poprogress").val().toFixed(2); //a2=70

    xArray.push([a1,"#fff"],[a2,"#fff"]);

2 Comments

The InsertIntoArray function seems pointless. Just use xArray.push([a1, '#fff']), etc.
@TedHopp i just thought that, this would be a cleaner way of doing this, Any way thanks for your attention. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.