Preiskonvertierung

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Max Merten

    #1

    Preiskonvertierung

    Hallo,

    ich habe Probleme mit der Berechnung von Preisen:

    bisherigerVKP = document.form.b isherigerVKP.va lue; Bspw: 200,20
    bisherigerEKP = document.form.b isherigerEKP.va lue; Bspw: 188,40

    neuerVKP = document.form.n euerVKP.value; Bspw: 250,00
    neuerEKP = document.form.n euerEKP.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
  • Evertjan.

    #2
    Re: Preiskonvertier ung

    Max Merten wrote on 14 mrt 2005 in comp.lang.javas cript:
    [color=blue]
    > ich habe Probleme mit der Berechnung von Preisen:
    >
    > bisherigerVKP = document.form.b isherigerVKP.va lue; Bspw: 200,20
    > bisherigerEKP = document.form.b isherigerEKP.va lue; Bspw: 188,40
    >
    > neuerVKP = document.form.n euerVKP.value; Bspw: 250,00
    > neuerEKP = document.form.n euerEKP.value; Bspw: 200,00
    >
    > zumeldenderVKP = neuerVKP - bisherigerVKP;
    > zumeldenderEKP = neuerEKP - bisherigerEKP;
    >[/color]

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

    zumeldenderVKP = c2p(neuerVKP) - c2p(bisherigerV KP)

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

    Comment

    • RobG

      #3
      Re: Preiskonvertier ung

      Evertjan. wrote:[color=blue]
      > Max Merten wrote on 14 mrt 2005 in comp.lang.javas cript:
      >
      >[color=green]
      >>ich habe Probleme mit der Berechnung von Preisen:
      >>
      >>bisherigerV KP = document.form.b isherigerVKP.va lue; Bspw: 200,20
      >>bisherigerE KP = document.form.b isherigerEKP.va lue; Bspw: 188,40
      >>
      >>neuerVKP = document.form.n euerVKP.value; Bspw: 250,00
      >>neuerEKP = document.form.n euerEKP.value; Bspw: 200,00
      >>
      >>zumeldenderVK P = neuerVKP - bisherigerVKP;
      >>zumeldenderEK P = neuerEKP - bisherigerEKP;
      >>[/color]
      >
      >
      > function c2p(x) {
      > return x.replace(/,/,'.')[/color]

      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';
      [color=blue]
      > }
      >
      > zumeldenderVKP = c2p(neuerVKP) - c2p(bisherigerV KP)
      >[/color]

      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.getEle mentById('resul t').innerHTML=
      doIt(this.value );">
      </label><br>
      <span id="result"></span>


      --
      Rob

      Comment

      • Deniz Adrian

        #4
        Re: Preiskonvertier ung

        Max Merten wrote:[color=blue]
        > Hallo,
        >
        > ich habe Probleme mit der Berechnung von Preisen:
        >
        > bisherigerVKP = document.form.b isherigerVKP.va lue; Bspw: 200,20
        > bisherigerEKP = document.form.b isherigerEKP.va lue; Bspw: 188,40
        >
        > neuerVKP = document.form.n euerVKP.value; Bspw: 250,00
        > neuerEKP = document.form.n euerEKP.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?[/color]

        So wie ich das sehe, solltest du einfach

        var bisherigerVKP = ...;

        schreiben anstatt

        bisherigerVKP = ...;

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

        gruss
        deniz

        Comment

        • Evertjan.

          #5
          Re: Preiskonvertier ung

          RobG wrote on 15 mrt 2005 in comp.lang.javas cript:
          [color=blue][color=green]
          >> function c2p(x) {
          >> return x.replace(/,/,'.')[/color]
          >
          > 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
          >[/color]

          In itself a good idea.

          In the context of the OQ a sideroad however.

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

          where comes this variable n from?

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

          Comment

          • Evertjan.

            #6
            Re: Preiskonvertier ung

            Deniz Adrian wrote on 15 mrt 2005 in comp.lang.javas cript:
            [color=blue]
            > Max Merten wrote:[color=green]
            >> Hallo,
            >>
            >> ich habe Probleme mit der Berechnung von Preisen:
            >>
            >> bisherigerVKP = document.form.b isherigerVKP.va lue; Bspw: 200,20
            >> bisherigerEKP = document.form.b isherigerEKP.va lue; Bspw: 188,40
            >>
            >> neuerVKP = document.form.n euerVKP.value; Bspw: 250,00
            >> neuerEKP = document.form.n euerEKP.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?[/color]
            >
            > So wie ich das sehe, solltest du einfach
            >
            > var bisherigerVKP = ...;
            >
            > schreiben anstatt
            >
            > bisherigerVKP = ...;
            >
            > Dann kannst du auch weiterhin auf die Variable zugreifen. :)[/color]

            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)

            Comment

            • RobG

              #7
              Re: Preiskonvertier ung

              Evertjan. wrote:[color=blue]
              > RobG wrote on 15 mrt 2005 in comp.lang.javas cript:
              >
              >[color=green][color=darkred]
              >>>function c2p(x) {
              >>> return x.replace(/,/,'.')[/color]
              >>
              >> 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
              >>[/color]
              >
              >
              > In itself a good idea.
              >
              > In the context of the OQ a sideroad however.[/color]

              The OQ relted to valid number formats, I figured it may help to
              reinforce valid formats with the OP's users too.
              [color=blue]
              >
              > the .replace() does not change the importance of checking.
              >
              >[color=green]
              >> return /^\d+\.\d\d$/.test(x))? x :
              >> n + '\nNummer nein gut';[/color]
              >
              >
              > where comes this variable n from?
              >[/color]

              Ooops, cut 'n paste from the subsequent code.


              --
              Rob

              Comment

              Working...