Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert cents value in this INTO WORDS conversion Javascript

I have a code which convert numerical value into words. I just want to add cents in words if user inputs an amount which have a cents in value.

ex. If I have an amount of 1,000.88. The value in words will be ONE THOUSAND PESOS AND EIGHTY EIGHY CENTS ONLY.

UPDATE: Code was edited to split the whole value and cents value. The only thing remaining is the conversion of cents into words.

FIDDLE

var NUMBER2TEXT = {
        ones: ['', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE', 'TEN', 'ELEVEN', 'TWELVE', 'THIRTEEN', 'FOURTEEN', 'FIFTEEN', 'SIXTEEN', 'SEVENTEEN', 'EIGHTEEN', 'NINETEEN'],
        tens: ['', '', 'TWENTY', 'THIRTY', 'FOURTY', 'FIFTY', 'SIXTY', 'SEVENTY', 'EIGHTY', 'NINETY'],
        sep: ['', ' THOUSAND ', ' MILLION ', ' BILLION ', ' TRILLION ', ' QUADRILLION ', ' QUINTILLION ', ' SEXTILLION ']
        };

        (function( ones, tens, sep ) {

        var input = document.getElementById( 'totalamountpaid' ),
        output = document.getElementById('words');

        input.onkeyup = function() {
        var val = this.value,
        arr = [],
        str = '',
        i = 0;

        if ( val.length === 0 ) {
        output.textContent = 'No amount paid';
        return;  
        }

        val = val.replace(/,/g,'');
        if ( isNaN( val ) ) {
        output.textContent = 'Invalid input.';
        return;   
        }


       val = val.toString();
       var valArray = val.split('\.', 2); //splits val into two seperate integers, whole numbers and decimals, in an array.   
       val = valArray[0]; //this is the whole number
       var val2 = valArray[1]; //this should be the decimals

        if(val2 != null && val2 != ''){
        //convert the decimals here
        var str2 = 'AND TWENTY CENTS';
        }else{
        var str2 = '';
        }

        while ( val ) {
        arr.push( val % 1000 );
        val = parseInt( val / 1000, 10 );   
        }

        while ( arr.length ) {
        str = (function( a ) {
        var x = Math.floor( a / 100 ),
        y = Math.floor( a / 10 ) % 10,
        z = a % 10;

        return ( x > 0 ? ones[x] + ' HUNDRED ' : '' ) +                 
        ( y >= 2 ? tens[y] + ' ' + ones[z] : ones[10*y + z] ); 
        })( arr.shift() ) + sep[i++] + str;                     
        }



        output.textContent = str + ' ' + str2 + ' PESOS ONLY';       
        };

        })( NUMBER2TEXT.ones, NUMBER2TEXT.tens, NUMBER2TEXT.sep );
like image 803
myRM Avatar asked Dec 26 '13 02:12

myRM


People also ask

How do you write numbers in words in Javascript?

var words = toWords(num); To get a number converted to words you just need to call the function passing it the number you want to convert and the corresponding words will be returned.

How do I print numbers in words in Javascript?

// numToWords :: (Number a, String a) => a -> String let numToWords = n => { let a = [ '', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' ]; let b = [ '', '', 'twenty', 'thirty', 'forty' ...

How are numbers converted to string in Javascript?

The toString() method is used with a number num as shown in above syntax using the '. ' operator. This method will convert num to a string. Parameters Used: This method accepts a single optional parameter base.


1 Answers

Sorry for the wait, for some reason this line was attacking my code.

val = parseInt( val.replace(/,/g,''), 10 );

I changed it to:

val = val.replace(/,/g,'');

which seems to have the same functionality while allowing me to do my thing. Here is the code. I use regexes to test for a period in val (the if statement), and then to split val into things before and after a period. The 2 in the split function should only allow for 2 strings to come out of it, meaning 10.20 would become '10' and '20' where 1.2.3 would become '1' and '2', and throw away 3. Arrays start at 0, so valArray[0] is the first string in the array, and valArray[1] is the second one.

val = val.toString();
var valArray = val.split('\.', 2); //splits val into two seperate integers, whole numbers and decimals, in an array.   
val = valArray[0]; //this is the whole number
var val2 = valArray[1]; //this should be the decimals

if(val2 != null && val2 != ''){
//convert the decimals here
var str2 = 'TWENTY CENTS';
}
else{
var str2 = '';
}

UPDATE: FIDDLE

like image 127
James G. Avatar answered Sep 30 '22 10:09

James G.