Announcement

Collapse
No announcement yet.

Splitting price

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Splitting price

    Hi

    An unusual request but i'm wondering if anyone has any suggestions or can point me in the right direction.

    I have a price for TY e.g. 100.235. I want to change the last 3 digits which are listed as a fraction into a decimal.

    My idea is to initially turn the price into a variable e.g. entryPrice.

    Then use entryPrice.toFixed(0) to get the whole number 100.

    Now I am stuck how do I grab the 3 numbers after the decimal point?

    If I could get this number I know how to manipulate the data into a decimal format.

    Thanks

  • #2
    thestranger
    Enclosed below is a code example showing one way to do it.
    For information on the String Object and its methods see this article in the EFS KnowledgeBase
    Alex

    PHP Code:
    var 100.235;
        
        var 
    a.toString();//convert the variable a to a string
        
    var b.indexOf(".")//find the index value of the decimal separator
        
    var b.substr(c+1,3);//creates new string of 3 characters starting from the index 
                                //after that of the decimal separator) 

        
    debugPrintln(d);//returns 235 

    Comment


    • #3
      Thanks Alexis. Your help is much appreciated!

      Comment


      • #4
        thestranger
        You are most welcome.
        Enclosed below is another way of doing it using this time the split method of the String Object.
        An advantage to this method is that you also have the integer portion (ie 100 in your example) readily available as c[0] (ie the first element of the array) without having to use entryPrice.toFixed(0).
        Alex

        PHP Code:
        var 100.235;
            
            var 
        a.toString();//convert the variable a to a string
            
        var b.split(".")//split the string at the dot (this converts it to an array) 
            
        var c[1];//assign to d the second element of the array which represents the 
                         //decimal portion of the number
            
            
        debugPrintln(d);//returns 235 

        Comment

        Working...
        X