0

I have two functions

function toCents(number) {
    number = number.replace("$","");
    arr = number.split(".");
    arr[0] = parseInt(arr[0]) * 100;
    if(arr[1].length === 1){
        arr[1] = parseInt(arr[1]) * 10;
    }else{
        arr[1] = parseInt(arr[1]);
    }
    number = arr[0] + arr[1];
    return number;
}

and

function centsToDollar(cents){
    cents = String(cents);
    position = cents.length - 2;
    return ["$", cents.slice(0,position), ".", cents.slice(position)].join("");
} 

is there a place for me to put these functions so that they will be available in every javascript file through out my rails application similar to how helper methods are used?

also is there a more standard way of converting dollars in string form to cents then back to dollar format with javascript?

2
  • 1
    Wouldn't storing them in your application.js be sufficient? Commented Dec 8, 2016 at 4:17
  • Maybe this can answer your question: stackoverflow.com/a/14221791/5725619 Commented Dec 8, 2016 at 4:40

0

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.