zwei files
This commit is contained in:
130
moneyUtils.js
Normal file
130
moneyUtils.js
Normal file
@@ -0,0 +1,130 @@
|
||||
// moneyUtils.js
|
||||
// Sammlung von Hilfsfunktionen für Geldbeträge (ohne reguläre Ausdrücke)
|
||||
// Die Funktionen werden an window.moneyUtils angehängt.
|
||||
|
||||
(function(global){
|
||||
'use strict';
|
||||
|
||||
function toNumberWithTwoDecimals(n){
|
||||
if(!Number.isFinite(n)) return NaN;
|
||||
return Number(n.toFixed(2));
|
||||
}
|
||||
|
||||
// leseGeldBetrag: liest eingabeText und wandelt in Number mit 2 Dezimalstellen um
|
||||
// Erlaubt Komma als Dezimaltrenner (z.B. "3,45" → 3.45)
|
||||
function leseGeldBetrag(eingabeText){
|
||||
if(eingabeText === null || eingabeText === undefined) return NaN;
|
||||
const s = String(eingabeText).trim();
|
||||
|
||||
// Ersetze alle Kommata durch Punkt (ohne Regex: split/join)
|
||||
const withDots = s.split(',').join('.');
|
||||
|
||||
// Baue einen String, der nur aus einer optionalen Vorzeichen, Ziffern und maximal einem Punkt besteht
|
||||
let resultChars = '';
|
||||
let seenDot = false;
|
||||
for(let i = 0; i < withDots.length; i++){
|
||||
const ch = withDots[i];
|
||||
// erlaubte Zeichen: 0-9, '.', '+' und '-' (nur am Anfang)
|
||||
if(ch >= '0' && ch <= '9'){
|
||||
resultChars += ch;
|
||||
} else if(ch === '.'){
|
||||
if(!seenDot){ resultChars += '.'; seenDot = true; }
|
||||
// weitere Punkte ignorieren
|
||||
} else if((ch === '+' || ch === '-') && resultChars.length === 0){
|
||||
// Vorzeichen nur akzeptieren, wenn es das erste Zeichen ist
|
||||
resultChars += ch;
|
||||
} else {
|
||||
// alle anderen Zeichen ignorieren (z.B. Währungen, Buchstaben, Leerzeichen)
|
||||
}
|
||||
}
|
||||
|
||||
if(resultChars.length === 0) return NaN;
|
||||
|
||||
const parsed = parseFloat(resultChars);
|
||||
if(!Number.isFinite(parsed)) return NaN;
|
||||
return toNumberWithTwoDecimals(parsed);
|
||||
}
|
||||
|
||||
// entferneZiffernUndSonderzeichen: entfernt Ziffern 0-9, Zeichen + - . , und Leerzeichen
|
||||
function entferneZiffernUndSonderzeichen(eingabeText){
|
||||
if(eingabeText === null || eingabeText === undefined) return '';
|
||||
const s = String(eingabeText);
|
||||
let out = '';
|
||||
for(let i = 0; i < s.length; i++){
|
||||
const ch = s[i];
|
||||
const isDigit = (ch >= '0' && ch <= '9');
|
||||
const isSpecial = (ch === '+' || ch === '-' || ch === '.' || ch === ',' || ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r');
|
||||
if(!isDigit && !isSpecial){
|
||||
out += ch;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Hilfsfunktion: prüft, ob der String Buchstaben enthält (a-z oder A-Z)
|
||||
function containsLetters(s){
|
||||
for(let i = 0; i < s.length; i++){
|
||||
const ch = s[i];
|
||||
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// istDollar: true, wenn Text in Number umwandelbar ist und $ / dollar / usd vorkommt (case-insensitive)
|
||||
function istDollar(textEingabe){
|
||||
if(textEingabe === null || textEingabe === undefined) return false;
|
||||
const s = String(textEingabe);
|
||||
const amount = leseGeldBetrag(s);
|
||||
if(!Number.isFinite(amount)) return false;
|
||||
const lower = s.toLowerCase();
|
||||
// '$' check: includes works for single char
|
||||
if(s.indexOf('$') !== -1) return true;
|
||||
if(lower.indexOf('dollar') !== -1) return true;
|
||||
if(lower.indexOf('usd') !== -1) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// istEuro: true, wenn Zahl parsen möglich und 'eur' oder 'euro' vorkommt (case-insensitive)
|
||||
// oder wenn Zahl parsebar und KEINE Buchstaben vorkommen
|
||||
function istEuro(eingabeText){
|
||||
if(eingabeText === null || eingabeText === undefined) return false;
|
||||
const s = String(eingabeText);
|
||||
const amount = leseGeldBetrag(s);
|
||||
if(!Number.isFinite(amount)) return false;
|
||||
const lower = s.toLowerCase();
|
||||
if(lower.indexOf('eur') !== -1) return true;
|
||||
if(lower.indexOf('euro') !== -1) return true;
|
||||
// Wenn keine Buchstaben vorhanden, akzeptiere (z. B. "1234.50")
|
||||
if(!containsLetters(s)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// dollarZuEuro: Umrechnung (1 USD = 0.83 EUR)
|
||||
function dollarZuEuro(dollarBetrag){
|
||||
const num = Number(dollarBetrag);
|
||||
if(!Number.isFinite(num)) return NaN;
|
||||
return toNumberWithTwoDecimals(num * 0.83);
|
||||
}
|
||||
|
||||
// leseGeldbetragInEuro: liest Betrag und wandelt ggf. von Dollar in Euro um
|
||||
function leseGeldbetragInEuro(eingabeText){
|
||||
const amount = leseGeldBetrag(eingabeText);
|
||||
if(!Number.isFinite(amount)) return NaN;
|
||||
if(istDollar(eingabeText)){
|
||||
return dollarZuEuro(amount);
|
||||
}
|
||||
// sonst: bereits Euro (oder keine Währungsangabe)
|
||||
return toNumberWithTwoDecimals(amount);
|
||||
}
|
||||
|
||||
// Exportiere die Funktionen
|
||||
global.moneyUtils = {
|
||||
leseGeldBetrag,
|
||||
entferneZiffernUndSonderzeichen,
|
||||
istDollar,
|
||||
istEuro,
|
||||
dollarZuEuro,
|
||||
leseGeldbetragInEuro
|
||||
};
|
||||
|
||||
})(window);
|
||||
Reference in New Issue
Block a user