Numbers to text
I am trying to find a way to convert my dollar amounts into text so that when a contract is generated from glowbiflow i am able to have the dollar amount in numbers and text. Is this even possible?
-
Hi David,
yes possible. This one returns 1,234 as
one thousand two hundred thirty-fourvar convert = String(Math.floor(@number field));
function int_to_words(int) {
if (int === 0) return 'zero';
var ONES = ['','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'];
var TENS = ['','','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety'];
var SCALE = ['','thousand','million','billion','trillion','quadrillion','quintillion','sextillion','septillion','octillion','nonillion'];
function get_first(str) {
return ('000' + str).substr(-3);
}
function get_rest(str) {
return str.substr(0, str.length - 3);
}
function triplet_to_words(_3rd, _2nd, _1st) {
return (_3rd == '0' ? '' : ONES[_3rd] + ' hundred ') + (_1st == '0' ? TENS[_2nd] : TENS[_2nd] && TENS[_2nd] + '-' || '') + (ONES[_2nd + _1st] || ONES[_1st]);
}
function add_to_words(words, triplet_words, scale_word) {
return triplet_words ? triplet_words + (scale_word && ' ' + scale_word || '') + ' ' + words : words;
}
function iter(words, i, first, rest) {
if (first == '000' && rest.length === 0) return words;
return iter(add_to_words(words, triplet_to_words(first[0], first[1], first[2]), SCALE[i]), ++i, get_first(rest), get_rest(rest));
}
return iter('', 0, get_first(String(int)), get_rest(String(int)));
};
var arr = convert.split(" ");
var converted = [];
for(var i = 0; i < arr.length; i++){
converted.push(isNaN(arr[i]) ? arr[i] : int_to_words(arr[i]," "));
};
converted.join(" ")Note 1: This code is based on the code provided by eat_chocolate, //https://stackoverflow.com/questions/14766951/convert-digits-into-words-with-javascript, I modified it a bit for the use in Podio.
Note 2: Decimals are rounded to floor: 1,234.99 will be rounded to 1,234 and that will be parsed to text, decimals won't be parsed to text.
Note 3: Replace @number field with your field where the amount is in.
Rainer -
Hi Rainer
is it only for international Numbering system which convert into text what if there is money field having multiple set currency option so if i select INR so the result format should come into that country numbering system.is it Possible to set dynamic value depending on that country numbering system in text format ???
-
Hi MD,
yes , that's possible.
But in a calculation field you don't have access to the currency, only to the amount (means: There's no currency variable token which you can put into the calculation) .
You would need a workaround. You can add a text field and enter the currency (which is set in the money field) there, then the calculation field "knows" the currency. Or you let a flow (Globiflow) enter the currency in the text field. Or if your setup allows this you have another indicator (address of clients or whatever) for which currency should be used.
In the calculation you need some IF conditions and some variables for each possible language/country.
Rainer
-
Found this but not sure how I'd use it in Globiflow / Procfu
<?php
/******************************************
*class to convert the number into french text
*******************************************/
class ConvertNumberToText{
private $unite = array("zÈro","un","deux","trois","quatre","cinq","six","sept","huit","neuf");
private $dix = array("dix","onze","douze","treize","quatorze","quinze","seize","dix sept","dix huit","dix neuf");
private $disaine = array(1=>"dix","vingt","trante","quarante","cinquante","soixante","soixante dix","quatre-vingt","quatre-vingt dix");
private $other = array(1=>"mille","million","milliard","billion","billiard","trillion","trilliard"/*,"quadrillion",
"quadrilliard","quintillion","quintilliard","sextillion","sextilliard","septillion",
"septilliard","octillion","octilliard","nonillion","nonilliard","dÈcillion","dÈcilliard"*/
);
public function Convert($number){
$number = preg_replace(array("/ /"),array(""),$number);
$string="";# var_dump($number);
$number = (string)$number; #echo $number."<br>";
if(strlen($number)>24) return "Le nombre ‡ traiter est trop grand";
#var_dump($number); echo "<br>";
$counter = strlen($number)/3; $to_be_converted = array(); $n=""; $count=0;
if(strlen($number)%3 == 0 && $counter !=0) $counter -= 1;
for($i=strlen($number)-1; $i>=0;$i--){
#echo $number[0]."<br>";
if($count != 0 && $count%3 == 0){
/* convert this and continue to the next if any */
$to_be_converted[$counter] = strrev($n);
$n = ""; $counter--;
}
$n .= $number[$i];
$count++;
}
$to_be_converted[$counter] = strrev($n);
//sort($to_be_converted);
#var_dump($to_be_converted); echo"<br>";
/* start conversation */
$switch = count($to_be_converted)-1;$nmbr="";# echo $switch;
for($i=0;$i<count($to_be_converted);$i++){
$nmbr .= $to_be_converted[$i];
$valid = false;
if(count($to_be_converted)-1 != $i) $nmbr .= " ";
/* each string will be converted independently */
$string .= ConvertNumberToText::convertHundrends($to_be_converted[$i],$valid);
/* add the range of counting */
if($valid){
#var_dump($this->other);
#echo $switch;
$string .= " ".@$this->other[$switch]." ";# break;
}
/* decrement the counter */
$switch--;
}
//var_dump($this->unite);
//echo strlen($number);
return ucfirst($string."(<b>".$nmbr."</b>)");
}
private function convertHundrends($number, &$valid){
#var_dump((int)$number); echo "<br>";
$rtn=""; $start = true;
if((int)$number != 0) $valid = true;
#var_dump($valid);
switch(strlen($number)){
case 3:
if($number[0] !=0){
if($number[0] == 1){
$rtn .= "cent ";
} else{
$rtn .= $this->unite[$number[0]]." cent ";
//$start = false;
}
}
$start = false;
case 2:
#echo $number[1];
if(!$start){
if($number[1] != 0){
if($number[1] != 1 && $number[1] != 7 && $number[1] != 9){
$rtn .= $this->disaine[$number[1]]." ";
}
}
} else{
if($number[0] != 0){
if($number[0] != 1 && $number[0] != 7 && $number[0] != 9){
$rtn .= $this->disaine[$number[0]]." ";
}
}
}
case 1:
if(@$number[2] != NULL){
if($number[1] == 1){
$rtn .= $this->dix[$number[2]];
} elseif($number[1] == 7){
$rtn .= "soixante ".$this->dix[$number[2]];
} elseif($number[1] == 9){
$rtn .= "quatre-vingt ".$this->dix[$number[2]];
} else{
if($number[2] !=0) $rtn .= $this->unite[$number[2]]." ";
}
} elseif(@$number[2] == NULL && @$number[1] != NULL){
if($number[0] == 1){
$rtn .= $this->dix[$number[1]];
} elseif($number[0] == 7){
$rtn .= "soixante ".$this->dix[$number[1]];
} elseif($number[0] == 9){
$rtn .= "quatre-vingt ".$this->dix[$number[1]];
} else{
$rtn .= $this->unite[$number[1]]." ";
}
} else{
$rtn .= $this->unite[$number[0]]." ";
}
default:
}/*
if(@$number[2] != "" && $number[0]!=0){
if($number[0] == 1) $rtn .="cent ";
else $rtn .= $this->unite[$number[0]]." cent "; #goto rtn;
}
//var_dump($number);
if(@$number[1] && $number[1] != 0 && $number[0] != ""){
//var_dump($number[1]);
if(@$number[2] != ""){
if($number[1] != 1) $rtn .= $this->disaine[$number[1]]." ";
elseif($number[1] != 1 && $number[2] !=0){
$rtn .= $this->disaine[$number[2]]." ";
goto rtn;
}
if($number[2] != 0) $rtn .= $this->unite[$number[2]]." ";
goto rtn;
}/*
if($number[0] != 1 && $number[0] != 9 && $number[0] != 7) $rtn .= $this->disaine[$number[0]]." ";
else{
if($number[0] == 7){
if($number[1] == 0) $rtn .= $this->disaine[$number[0]]." ";
elseif($number[1]<=6) $rtn .= "soixante ".$this->dix[$number[1]];
goto rtn;
} elseif($number[0] == 0){
$rtn .= $this->disaine[$number[0]]." ";
goto rtn;
} elseif($number[0]<6){
$rtn .= $this->dix[$number[0]]." ";
goto rtn;
} else{
$rtn .= "dix-".$this->unite[$number[0]]." ";
goto rtn;
}
}/
}
if(@$number[1] == null){
if($number[0] != null) $rtn .= $this->unite[$number[0]]." ";
}*/
rtn:
return $rtn;
}
}
?>
Please sign in to leave a comment.
Comments
6 comments