473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Preiskonvertierung

Hallo,

ich habe Probleme mit der Berechnung von Preisen:

bisherigerVKP = document.form.bisherigerVKP.value; Bspw: 200,20
bisherigerEKP = document.form.bisherigerEKP.value; Bspw: 188,40

neuerVKP = document.form.neuerVKP.value; Bspw: 250,00
neuerEKP = document.form.neuerEKP.value; Bspw: 200,00

zumeldenderVKP = neuerVKP - bisherigerVKP;
zumeldenderEKP = neuerEKP - bisherigerEKP;
Jetzt bekomme ich bei den unteren beiden Variablen immer ein "NAN".

Wie gehe ich damit am besten um?

Gruß
Max
Jul 23 '05 #1
6 1170
Max Merten wrote on 14 mrt 2005 in comp.lang.javascript:
ich habe Probleme mit der Berechnung von Preisen:

bisherigerVKP = document.form.bisherigerVKP.value; Bspw: 200,20
bisherigerEKP = document.form.bisherigerEKP.value; Bspw: 188,40

neuerVKP = document.form.neuerVKP.value; Bspw: 250,00
neuerEKP = document.form.neuerEKP.value; Bspw: 200,00

zumeldenderVKP = neuerVKP - bisherigerVKP;
zumeldenderEKP = neuerEKP - bisherigerEKP;


function c2p(x) {
return x.replace(/,/,'.')
}

zumeldenderVKP = c2p(neuerVKP) - c2p(bisherigerVKP)

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #2
Evertjan. wrote:
Max Merten wrote on 14 mrt 2005 in comp.lang.javascript:

ich habe Probleme mit der Berechnung von Preisen:

bisherigerVKP = document.form.bisherigerVKP.value; Bspw: 200,20
bisherigerEKP = document.form.bisherigerEKP.value; Bspw: 188,40

neuerVKP = document.form.neuerVKP.value; Bspw: 250,00
neuerEKP = document.form.neuerEKP.value; Bspw: 200,00

zumeldenderVKP = neuerVKP - bisherigerVKP;
zumeldenderEKP = neuerEKP - bisherigerEKP;
function c2p(x) {
return x.replace(/,/,'.')


The modified value should be checked to ensure it's a number
before putting it back in the page.

x = x.replace(/,/,'.');

// Test x is digits with two digits after the decimal point
return /^\d+\.\d\d$/.test(x))? x :
n + '\nNummer nein gut';
}

zumeldenderVKP = c2p(neuerVKP) - c2p(bisherigerVKP)


Excuse my reply in English, I haven't tried Deutsch since
school...

IIRC, Deutchlanders like to write numbers: 23.234.234,00
where the English (Roman?) numbering system would have written:
23,234,234.00.

The following script attempts to remove thousands separators and
ensure the decimal point is a '.' and not a ','. It looks to
see if a '.' comes before a ','. If so, it removes all '.' and
changes the ',' to a '.'.

If a comma comes before a period, it just removes the commas.

Lastly, it tests to see if the final number is only digits
followed by a '.' and one or more digits.

There should also be an on-screen tip that suggests the
appropriate format (as indicated in the HTML).

It is impossible to be exhaustive when validating this way
unless you only allow one format or the other. If there is only
one '.' or one ',' it's impossible to test if it's a decimal
point with three places or a thousands separator.

<script type="text/javascript">
function doIt(n){
var x = n;
if (x.lastIndexOf('.') < x.lastIndexOf(',')){
x = x.replace(/\./g,'').replace(/,/,'.');
} else {
x = x.replace(/,/g,'');
}
// Check it's a reasonable number
return (/^\d+\.\d+$/.test(x))? x :
n + '\nInput not acceptable';
}
</script>

<label for="numberSlot">Please enter a value
(e.g. 23.000,00)<br>
<input type="text" width="50" id="numberSlot" onblur="
document.getElementById('result').innerHTML=
doIt(this.value);">
</label><br>
<span id="result"></span>
--
Rob
Jul 23 '05 #3
Max Merten wrote:
Hallo,

ich habe Probleme mit der Berechnung von Preisen:

bisherigerVKP = document.form.bisherigerVKP.value; Bspw: 200,20
bisherigerEKP = document.form.bisherigerEKP.value; Bspw: 188,40

neuerVKP = document.form.neuerVKP.value; Bspw: 250,00
neuerEKP = document.form.neuerEKP.value; Bspw: 200,00

zumeldenderVKP = neuerVKP - bisherigerVKP;
zumeldenderEKP = neuerEKP - bisherigerEKP;
Jetzt bekomme ich bei den unteren beiden Variablen immer ein "NAN".

Wie gehe ich damit am besten um?


So wie ich das sehe, solltest du einfach

var bisherigerVKP = ...;

schreiben anstatt

bisherigerVKP = ...;

Dann kannst du auch weiterhin auf die Variable zugreifen. :)

gruss
deniz
Jul 23 '05 #4
RobG wrote on 15 mrt 2005 in comp.lang.javascript:
function c2p(x) {
return x.replace(/,/,'.')
The modified value should be checked to ensure it's a number
before putting it back in the page.

x = x.replace(/,/,'.');

// Test x is digits with two digits after the decimal point


In itself a good idea.

In the context of the OQ a sideroad however.

the .replace() does not change the importance of checking.
return /^\d+\.\d\d$/.test(x))? x :
n + '\nNummer nein gut';


where comes this variable n from?

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #5
Deniz Adrian wrote on 15 mrt 2005 in comp.lang.javascript:
Max Merten wrote:
Hallo,

ich habe Probleme mit der Berechnung von Preisen:

bisherigerVKP = document.form.bisherigerVKP.value; Bspw: 200,20
bisherigerEKP = document.form.bisherigerEKP.value; Bspw: 188,40

neuerVKP = document.form.neuerVKP.value; Bspw: 250,00
neuerEKP = document.form.neuerEKP.value; Bspw: 200,00

zumeldenderVKP = neuerVKP - bisherigerVKP;
zumeldenderEKP = neuerEKP - bisherigerEKP;
Jetzt bekomme ich bei den unteren beiden Variablen immer ein "NAN".

Wie gehe ich damit am besten um?


So wie ich das sehe, solltest du einfach

var bisherigerVKP = ...;

schreiben anstatt

bisherigerVKP = ...;

Dann kannst du auch weiterhin auf die Variable zugreifen. :)


No, that is not the problem.
The problem is the comma, which is not the decimal divider of javascript.
See other posting for solutions.

"var" only declares a new variable inside a function, that is distinct of
a global one with the same name. In all other cases it is default and can
be left out. Forget about the "weiterhin zugreifen".
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #6
Evertjan. wrote:
RobG wrote on 15 mrt 2005 in comp.lang.javascript:

function c2p(x) {
return x.replace(/,/,'.')
The modified value should be checked to ensure it's a number
before putting it back in the page.

x = x.replace(/,/,'.');

// Test x is digits with two digits after the decimal point

In itself a good idea.

In the context of the OQ a sideroad however.


The OQ relted to valid number formats, I figured it may help to
reinforce valid formats with the OP's users too.

the .replace() does not change the importance of checking.

return /^\d+\.\d\d$/.test(x))? x :
n + '\nNummer nein gut';

where comes this variable n from?


Ooops, cut 'n paste from the subsequent code.
--
Rob
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.