anoniem Geplaatst: 8 februari 2002 Delen Geplaatst: 8 februari 2002 In javascript probeer ik uit een aantal strings de prijs te halen door de locatie van het woord 'Euro:' vast te stellen. Daarna pak ik het gedeelte voor de komma * 100 + het gedeelte na de komma. Hoe zet ik nu het eindresultaat weer om in een geld-string. Dus nu heb ik 12,25 + 14,36 = 2661 dat moet worden 26,61 Ik heb met een hoop klooien onderstaand script gemaakt, dat laatste wilt maar niet lukken. Als iemand een heel andere oplossing heeft dan is dat ook welkom. In het kort komt het er op neer dat ik dus een getal moet omzetten in een string. [code:1:dfcc88cb55] function GetPrice(A_string) { var charteller=0; var notfound=true; var left = ''; var right = ''; for (charteller==1; (A_string.length-1); charteller++) { if ((A_string.charAt(charteller)=="o") && (A_string.charAt(charteller-3)=="E") && (A_string.charAt(charteller+1)==":")) { notfound=false; left = parseInt(A_string.substring(charteller+2,A_string.length-2)); left = left * 100; right = parseInt(A_string.substring(A_string.length-2,A_string.length)); left = left + right; return left; break; } } if (notfound) return 0; } function berekenprijs() { var price = 0; for (var i=0; i<top.frames[0].artArray.length; i++) { for (var j=1; j<=top.frames[0].aanArray[i]; j++) price = price + (GetPrice(top.frames[0].artArray[i])); } return price } [/code:1:dfcc88cb55] Quote Link naar reactie
anoniem Geplaatst: 8 februari 2002 Auteur Delen Geplaatst: 8 februari 2002 Even kijken je er left = left + '.' + right; van maken. dan zit je natuurlijk met het probleem dat je dan weer een char hebt. deze moet je dan weer omzetten. Ik moet eerlijk bekennen dat ik even niet weet welke functie daarvoor gebruikt moet worden. vrgr, william Quote Link naar reactie
anoniem Geplaatst: 8 februari 2002 Auteur Delen Geplaatst: 8 februari 2002 Het eindantwoord kan je omzetten naar integer en daarna delen door 100. geld = parseInt(eindresultaat)/100; evt. kan je de . weer omzetten naar een , met: geld = (parseInt(eindresultaat)/100;).toString().replace(/./g, ','); btw. ik probeerde laatst ook m'n geld te tellen in javascript, maar ik kreeg een integer-overflow :wink: [ Dit Bericht is bewerkt door: Annie op 2002-02-08 11:17 ] Quote Link naar reactie
anoniem Geplaatst: 9 februari 2002 Auteur Delen Geplaatst: 9 februari 2002 Als ik de functie maak zoals jij schrijft dan werkt het niet. Er werkt dan geen enkele javascript functie meer in het document. Staat er misschien iets fout? [code:1:d8c5367a86] function geld() { var price = 0; for (var i=0; i<top.frames[0].artArray.length; i++) { for (var j=1; j<=top.frames[0].aanArray[i]; j++) price = price + (GetPrice(top.frames[0].artArray[i])); } geld = (parseInt(price)/100;).toString().replace(/./g,',');} [/code:1:d8c5367a86] Ik heb zelf al zitten testen met haakjes en puntjes maar ik kom er niet uit. Quote Link naar reactie
anoniem Geplaatst: 9 februari 2002 Auteur Delen Geplaatst: 9 februari 2002 [quote:e17fc8cdb4] Op 09-02-2002 19:41, schreef Niek Breur: Als ik de functie maak zoals jij schrijft dan werkt het niet. Er werkt dan geen enkele javascript functie meer in het document. Staat er misschien iets fout? geld = [b:e17fc8cdb4](parseInt(price)/100;)[/b:e17fc8cdb4].toString().replace(/./g,',');} [/quote:e17fc8cdb4] Even die puntkomma in het vetgedrukte deel weghalen. [ Dit Bericht is bewerkt door: Bigfoot op 2002-02-09 19:50 ] Quote Link naar reactie
anoniem Geplaatst: 9 februari 2002 Auteur Delen Geplaatst: 9 februari 2002 Ik had dat ook al even getest, maar dan krijg ik ,,,, als antwoord. Quote Link naar reactie
anoniem Geplaatst: 9 februari 2002 Auteur Delen Geplaatst: 9 februari 2002 oeps foutje. dit zal beter werken: geld = (parseInt(eindresultaat)/100).toString().replace(/./g, ','); Quote Link naar reactie
anoniem Geplaatst: 9 februari 2002 Auteur Delen Geplaatst: 9 februari 2002 Dat werkt inderdaad veel beter. Probleem opgelost. Hartelijk dank. Quote Link naar reactie
anoniem Geplaatst: 11 februari 2002 Auteur Delen Geplaatst: 11 februari 2002 Bijna opgelost. Het werkt fantastisch behalve als het nette hele guldens zijn. De komma en de nullen achteraan ontbreken dan. Kan dat ook op een eenvoudige manier? Kun je de string een soort van format meegeven? Quote Link naar reactie
anoniem Geplaatst: 12 februari 2002 Auteur Delen Geplaatst: 12 februari 2002 De eenvoudige manier is kijken naar de lengte van de string na de komma. lengte = 0 --> string + ",00" lengte = 1 --> string + "0" Een moeilijke manier heb ik ook :smile: Heb laatst geprobeerd om een sprintf() functie te bouwen voor javascript. Het resultaat staat hieronder: [code:1:9ceb653c1a] function sprintf() { /* ====================================================================== sprintf() ====================================================================== Purpose : format a string Author : Antoine Hurkmans, January 2002 ---------------------------------------------------------------------- Parameters : So, you're saying you don't know what sprintf() does? Tsss, go buy a book. ---------------------------------------------------------------------- Returns : a string formatted according to first parameter ---------------------------------------------------------------------- Revision History : 12-Feb-02 AH - Added support for alternate padding char 05-Feb-02 AH - Fixed bug in display of decimal part for floats 28-Jan-02 AH - Initial Version ====================================================================== */ var iCount, iPadLength, aMatch, iMatchIndex = 1; var bAlignLeft, sPad, iWidth, iPrecision, sType; var aArgs = sprintf.arguments; if (aArgs.length < 2) return ''; var sFormat = aArgs[0]; var re = /%(-)?(0| |'.)?(d+)?(.d*)?([bcdfosxX]{1})/; while (re.test(sFormat)) { aMatch = re.exec(sFormat); bAlignLeft = (aMatch[1] == '-'); sPad = (aMatch[2] == '' ? ' ' : aMatch[2]); if (sPad.substring(0, 1) == "'") sPad = sPad.substring(1); iWidth = (aMatch[3] > 0 ? parseInt(aMatch[3]) : 0); iPrecision = (aMatch[4].length > 1 ? parseInt(aMatch[4].substring(1)) : 6); sType = aMatch[5]; mArgument = (aArgs[iMatchIndex] != null ? aArgs[iMatchIndex] : ''); ++iMatchIndex; if (mArgument.toString().length) { if ('fbcdoxX'.indexOf(sType) != -1 && isNaN(mArgument)) mArgument = 0; switch (sType) { case 'f': // floats var iPower = Math.pow(10, iPrecision); mArgument = (Math.round(parseFloat(mArgument) * iPower) / iPower).toString(); var aFloatParts = mArgument.split('.'); if (iPrecision > 0) { if (aFloatParts.length == 1) aFloatParts[1] = ''; // pad with zeroes to precision for (iCount = aFloatParts[1].length; iCount < iPrecision; iCount++) aFloatParts[1] += '0'; mArgument = aFloatParts[0] + '.' + aFloatParts[1]; } else mArgument = aFloatParts[0]; iPadLength = aFloatParts[0].length; break; case 'b': // binary mArgument = parseInt(mArgument).toString(2); iPadLength = mArgument.length; break; case 'c': // character mArgument = String.fromCharCode(parseInt(mArgument)); break; case 'd': // decimal mArgument = mArgument.toString(); iPadLength = mArgument.length; break; case 'o': // octal mArgument = parseInt(mArgument).toString(8); iPadLength = mArgument.length; break; case 'x': // hexadecimal (lowercase) mArgument = parseInt(mArgument).toString(16); iPadLength = mArgument.length; break; case 'X': // hexadecimal (uppercase) mArgument = parseInt(mArgument).toString(16).toUpperCase(); iPadLength = mArgument.length; break; default: // strings mArgument = mArgument.toString(); iPadLength = mArgument.length; } if ('fbdoxX'.indexOf(sType) != -1) { // pad with padding-char to width if (bAlignLeft) for (iCount = iPadLength; iCount < iWidth; iCount++) mArgument += sPad; else for (iCount = iPadLength; iCount < iWidth; iCount++) mArgument = sPad + mArgument; } } sFormat = sFormat.replace(re, mArgument); } return sFormat; } [/code:1:9ceb653c1a] /edit: oh ja, als de functie bugs bevat, veel succes met het aanpassen, ik geef geen support :wink: [ Dit Bericht is bewerkt door: Annie op 2002-02-12 22:07 ] Quote Link naar reactie
anoniem Geplaatst: 12 februari 2002 Auteur Delen Geplaatst: 12 februari 2002 copy&paste en hup in de snippets.. bedankt Annie! :smile: Quote Link naar reactie
anoniem Geplaatst: 12 februari 2002 Auteur Delen Geplaatst: 12 februari 2002 Hmmmmmm, en nog even voor de dummy's onder ons...... (daar val ik wel onder als ik dit zie) Hoe roep ik dit aan? Quote Link naar reactie
anoniem Geplaatst: 12 februari 2002 Auteur Delen Geplaatst: 12 februari 2002 Daar geef ik je de uitleg van php voor: [quote:b88e220d01] sprintf (PHP 3, PHP 4 ) sprintf -- Return a formatted string Description string sprintf (string format [, mixed args...]) Returns a string produced according to the formatting string format. The format string is composed of zero or more directives: ordinary characters (excluding %) that are copied directly to the result, and conversion specifications, each of which results in fetching its own parameter. This applies to both sprintf() and printf(). Each conversion specification consists of a percent sign (%), followed by one or more of these elements, in order: An optional padding specifier that says what character will be used for padding the results to the right string size. This may be a space character or a 0 (zero character). The default is to pad with spaces. An alternate padding character can be specified by prefixing it with a single quote ('). See the examples below. An optional alignment specifier that says if the result should be left-justified or right-justified. The default is right-justified; a - character here will make it left-justified. An optional number, a width specifier that says how many characters (minimum) this conversion should result in. An optional precision specifier that says how many decimal digits should be displayed for floating-point numbers. This option has no effect for other types than double. (Another function useful for formatting numbers is number_format().) A type specifier that says what type the argument data should be treated as. Possible types: % - a literal percent character. No argument is required. b - the argument is treated as an integer, and presented as a binary number. c - the argument is treated as an integer, and presented as the character with that ASCII value. d - the argument is treated as an integer, and presented as a decimal number. f - the argument is treated as a double, and presented as a floating-point number. o - the argument is treated as an integer, and presented as an octal number. s - the argument is treated as and presented as a string. x - the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters). X - the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters). See also: printf(), sscanf(), fscanf(), and number_format(). Examples Example 1. Sprintf(): zero-padded integers $isodate = sprintf ("%04d-%02d-%02d", $year, $month, $day); Example 2. Sprintf(): formatting currency $money1 = 68.75; $money2 = 54.35; $money = $money1 + $money2; // echo $money will output "123.1"; $formatted = sprintf ("%01.2f", $money); // echo $formatted will output "123.10" [/quote:b88e220d01] Quote Link naar reactie
anoniem Geplaatst: 12 februari 2002 Auteur Delen Geplaatst: 12 februari 2002 Ik zie trouwens dat het forum de regexp vernaggeld had na m'n edit. Backslashes waren ineens weg. Heb de code nogmaals gepost (en meteen een functionaliteit toegevoegd die ik vergeten was). [ Dit Bericht is bewerkt door: Annie op 2002-02-12 22:04 ] Quote Link naar reactie
Aanbevolen berichten
Om een reactie te plaatsen, moet je eerst inloggen