variable in euros weergeven

  • Onderwerp starter Onderwerp starter wake
  • Startdatum Startdatum
Status
Niet open voor verdere reacties.

wake

Gebruiker
Lid geworden
21 aug 2007
Berichten
195
ik wil een variable '2309932'
omtoveren naar:

2.309.932,00

kan iemand mij hiermee opweg helpen?
 
je zou het om kunnen zetten naar een string
Ik hoop dat ik het zo goed doe:
Code:
$string=var+',00'

en als je door wil rekenen verder gewoon die var gerbuiken, en later weer die ,00 eraan toevoegen.

ik weet niet of dit een oplossing is voor jou of dat het echt een var moet blijven.
dit is in ieder geval wat ik kan bedenken ;)
 
ik heb zelf een javascript geschreven wat bijvoorbeeld een text in een inputbox van '0128a,3324.3c234' veranderd naar '1.283.324,32'. Hiermee kun je dus een foutgetypte valua omzetten naar een goede..

de inputbox..:
PHP:
Bedrag: <input type="text" name="bedrag" size="30" onblur="valuta_translate(this)" >


het jscript:
PHP:
<script type="text/javascript">

// =======================
//     Script written by:
//   Bo Pennings AKA Wake
//     www.bopennings.com
// =======================


function valuta_translate(field)
{
	with (field)
	{
		behind_comma = '' ;// empties the behind comma string
		befront_comma = '';// empties the behind dot string
		last = -1;
		input = field.value;  /// puts the inputbox value to the string 'input'
		last_commapos=value.lastIndexOf(","); // declares the position of the last comma
		last_dotpos=value.lastIndexOf("."); // declares the position of the last dot
				
				
		if (last_dotpos > last_commapos ) // looks what char (dot or comma) is last in the string and puts the position to string 'last'
			{ last = last_dotpos; }
		else if (last_commapos > last_dotpos )
			{ last = last_commapos;}
						
		if (last >= 0){ // if there is a comma...
		behind_comma = input.substring(last + 1 , input.length);} // it puts the chars behind the comma in variable 'behind_comma'
		else   // if there is no comma or dot.. it sets behind_comma to '0'
		{behind_comma = '0';
		 last = input.length;}   // sets last to the input length if there isnt a comma or dot, for returning the complete input
		
		befront_comma = input.substring(0 , last); // declares the chars in front of the comma
		
		if (befront_comma == 0) // looks if a value is set in front of the comma, else it uses '0' to show 0,xx instead of ,xx
		{befront_comma = '0';}
		
		//// the chars that are not allowed in the field.. (they will be stripped from the string)
		var replacers = new Array('\\.',',','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','!'
		,'@','#','\\$','%','\\^','\\&','\\*','\\(','\\)','\\-','\\+','\\{','\\}','\\|','\\:','\\;','\\<','\\>','\\?','\\~','\\`','\\[','\\]');

		i = 0;
		while(i<replacers.length){  // trims the input from any un-allowed chars
			var replacer_value = replacers[i];
			var replacer_value = new RegExp(replacer_value, 'gi');
									
			befront_comma = (befront_comma.replace(replacer_value,"")) ;
			behind_comma = (behind_comma.replace(replacer_value,""));
			i++;
		}
		
		
		function repeat(string, nummer){ // this function is in use for adding zeros behind the comma
			oldstring = string;
			newstring = "";
			i = 0;
			for (i = 0; i < nummer; i++)
				{newstring = newstring += oldstring;}
			return newstring;
		};

		behind_comma = behind_comma.substring(0 , 2) // declares the first 2 chars for the value behind the comma
		
		if (behind_comma.length < 2){   // this adds zeros to the string behind the comma if it's les then two... 
		behind_comma += repeat('0',(2 - behind_comma.length));} 
		
		while (befront_comma.substring(0,1) == '0' && befront_comma.length > 1) // removes zeros in the first char of the input
		{befront_comma = befront_comma.substring(1,befront_comma.length)}
		
		var befront_comma_length = '';
		var lastthree = '';
		var befront_comma_dotted = '';
		
		while (befront_comma.length > 3){  // adds dots to the string (example: 1023403 change to  1.023.403)
		befront_comma_length = befront_comma.length;
		lastthree = befront_comma.substring(befront_comma_length, befront_comma_length - 3);
		befront_comma = befront_comma.substring(0, befront_comma_length - 3);
		lastthree = '.' + lastthree;
		befront_comma_dotted =  lastthree  + befront_comma_dotted;
		}
		befront_comma = befront_comma + befront_comma_dotted;
		
		
		field.value = befront_comma + ',' + behind_comma; // puts the new input in the box


		
	}
}


// end of script..
</script>
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan