0

I have a variable listed as %SUBTOTAL% in an HTML document, if I just put %SUBTOTAL% it is replaced by a number, in this case "551,313.46". If I write this:

<script type="text/javascript">document.write(%SUBTOTAL%);</script>

it is replaced by "551313.46"
I want to do basic math operations with that number but whrn I try to multiply it by 2

<script type="text/javascript">document.write(%SUBTOTAL%*2);</script>

I just get this "551626.92" so, only tne numbers after the coma are multilplied by 2, how can I get the whole number to operate?

Andrew
  • 13,757
  • 13
  • 66
  • 84
  • 2
    Could you please post the *generated* HTML, where the value is already replaced? I'm especially wondering why, if the value is `551,313.46`, it shows `551313.46` (i.e. without a comma). *If* the generated code was `document.write("551,313.46");`, it should show the comma. If it was `document.write(551,313.46);`, it should show `313.56` only. – Felix Kling Jan 20 '14 at 17:51
  • 1
    What is `%SUBTOTAL%`, how is it defined, and what is parsing it to create a numeric value? – freefaller Jan 20 '14 at 17:52
  • `%` is an [invalid character](http://stackoverflow.com/q/1661197/1216976) for a JavaScript variable name. Try using an underscore. – SomeKittens Jan 20 '14 at 17:52
  • @SomeKittensUx2666 reread the question, hes not using it as a variable name. Thats being replaced before it reaches the end user. – castis Jan 20 '14 at 17:54
  • @castis I see [tag:javascript] and [tag:html]. He's also operating on them as if it's JS. – SomeKittens Jan 20 '14 at 17:55
  • I understand you see javascript and html, please try to see the words he's used to explain his question. – castis Jan 20 '14 at 17:56
  • this is what ive tried https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-ash4/t1/1606890_10151854861881932_199601495_n.jpg – Axel Mejía Jan 20 '14 at 18:06
  • Please do not update your question with an answer, instead, either select an answer from below by clicking the check mark next to it, or post your own answer. – Andrew Jan 20 '14 at 19:22

4 Answers4

3

The problem here is the fact a comma in a number is not a thousands seperator, it is an operator.

What the browser sees is

(expresion 1), (expression 2)

You can not spit out the number like that and get it to work. You will need to remove the comma for the operation to take place.

And playing with code looks like some browsers let you pass in multiple arguments into document.write and it will write them out

document.write("A","B","C","D");

results in ABCD being written out, which is why you see the results.

Something like this would work

document.write(parseFloat("%SUBTOTAL%".replace(/,/g,"")*2));

and if you want the comma to be inserted again, you would need to add code to insert it.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • That's what I thought, but it doesn't explain the output of `"551313.46"`... or maybe I misunderstood that part. – Felix Kling Jan 20 '14 at 17:56
  • 2
    Looks like `document.write` can accept multiple arguments as a comma separated list and writes them out. – epascarello Jan 20 '14 at 18:00
  • OH! That's it... sneaky :) I was so focused on the comma operator that I didn't consider it as argument separator. – Felix Kling Jan 20 '14 at 18:01
  • ohhh, now it all makes sense, thanks, but now the question is, how to remove the comma? i cant modify the other program to replace it for thr number but without a comma and i cant know for sure how many of them will there be, sometimes it could be relaced by just 5 or sometimes for 5,214,147,453.25 – Axel Mejía Jan 20 '14 at 18:12
  • i tried document.write(parseFloat("%SUBTOTAL%".replace(/,/g,"")*2); and document.write(parseFloat(%SUBTOTAL%.replace(/,/g,"")*2); either way it writes nothing – Axel Mejía Jan 20 '14 at 18:16
  • 1
    This answer is missing a close parenthesis :) – nderscore Jan 20 '14 at 18:21
  • added the missing ) to the end. That is what happens when eating a sandwich and typing with one hand. – epascarello Jan 20 '14 at 18:39
1

The comma is splitting your number into two separate values which are being passed as seperate arguments to document.write. The solution is to remove the comma. The best solution would be to change it server side.

If you absolutely must, you could modify your code so that JavaScript removes it.

document.write("%SUBTOTAL%".replace(/[^0-9.]/g,'') * 2);

/[^0-9.]/g will match all characters that are not 0-9 or a period. Replace them with empty string to remove them. Multiplying by two will cast the string to a number and multiply.

nderscore
  • 4,182
  • 22
  • 29
0

This is not a problem in javascript, it's a problem in whatever server-side language you're using. You need to make sure %SUBTOTAL% is formatted as a number, not a string.

Here is what is currently being passed to the front-end code: 551,313.46

When you multiply by two, here's what it shows up as: 551,313.46*2

You should use a ParseFloat function or similar in your SERVER SIDE code, hopefully one which accounts for commas. Since this is a string on the server-side, you may just be able to remove the comma with a string replace function, a-la SUBTOTAL.replace(',',''), but until you show your server-side code, there's really no way to answer the question effectively.

If you're dead-set on doing this in the client-side, not providing us with your server-side source, and forcing us to guess at your problem; here's a very bad hack to work around the issue you're seeing:

Try wrapping %SUBTOTAL% with quotes: document.write(ParseFloat("%SUBTOTAL%")*2)

gcochard
  • 11,408
  • 1
  • 26
  • 41
  • Why was this downvoted? You clearly didn't read or understand my answer. – gcochard Jan 20 '14 at 17:57
  • *"Right now it's being interpreted as a string"* No it isn't. It looks like the generated code is `document.write(551, 313.46);`, i.e. a function call with two arguments. – Felix Kling Jan 20 '14 at 18:03
  • @FelixKling You're right. I was thinking that it was interpreted as a string by the server-side code, since he uses %SUBTOTAL%*2, but looks like it is sending this to the client: `(551,313.46*2)`. – gcochard Jan 20 '14 at 18:06
  • Oh, you thought that the `*2` was performed on the server? Now I understand. – Felix Kling Jan 20 '14 at 18:07
-1

Try doing something like document.write(parseFloat(%SUBTOTAL%)*2)

nFinIt_loop
  • 1,463
  • 2
  • 9
  • 12