Simple Machines Community Forum

General Community => Scripting Help => Topic started by: MoreBloodWine on March 30, 2018, 04:19:19 PM

Title: Need a quick bit of help with some JS, Ty.
Post by: MoreBloodWine on March 30, 2018, 04:19:19 PM
When the total is called, it gives the expected result but has a period next to it. How can this period be removed ?

See this (http://spend-ur-bits.com/index.php?page=egc_lottery) for what I'm talking about. Subset of line 3.

Edit: Not my code, goes back a few years.

<script language="javascript">
var cost = {COST};
var multiplier = 3;
var total = (cost*multiplier).toFixed(4).replace(/0+$/,'');
</script>

Title: Re: Need a quick bit of help with some JS, Ty.
Post by: MoreBloodWine on March 30, 2018, 05:45:20 PM
Seem to have fixed it by changing the 4 to  0.
Title: Re: Need a quick bit of help with some JS, Ty.
Post by: MoreBloodWine on March 30, 2018, 08:21:27 PM
So I marked this unsolved bec I thought of something I didnt initially consider. But best I can tell, the original code hides the 4 trailing 0's. Which is fine. But if I change the "ticket" price on my site to say 0.5. That subset of line 3 will show just a 1, because my change will hide everything. So going back to the original code. How can that decimal be hidden until it's otherwise needed. I hope this all makes sense, Ty.
Title: Re: Need a quick bit of help with some JS, Ty.
Post by: MoreBloodWine on April 11, 2018, 05:21:09 PM
? Anyone ?

No rush, just trying not to forget about it lol
Title: Re: Need a quick bit of help with some JS, Ty.
Post by: Chen Zhen on April 11, 2018, 05:32:35 PM
I'm not exactly sure about what you are attempting to accomplish.
If you are attempting to remove a decimal from a number you can possibly use one of the following:


Math.round()
Math.floor()
Math.trunc()


.. or perhaps you just want to remove any single character at the end of the variable?


total.replace(/.$/,"");
Title: Re: Need a quick bit of help with some JS, Ty.
Post by: MoreBloodWine on April 11, 2018, 06:26:35 PM
Quote from: Chen Zhen on April 11, 2018, 05:32:35 PM
I'm not exactly sure about what you are attempting to accomplish.
If you are attempting to remove a decimal from a number you can possibly use one of the following:


Math.round()
Math.floor()
Math.trunc()


.. or perhaps you just want to remove any single character at the end of the variable?


total.replace(/.$/,"");

Based on other variables that come in to play, there may or may not be digits after the seen decimal. BUT, if there are no trailing digits to show other than 0's which are hidden. The decimal still shows. The code as it sits, does the job just fine. But when there are no trailing digits to show, the decimal is still shown which I'd prefer it not show, until it's needed.

Not sure if I can really describe this any other way heh
Title: Re: Need a quick bit of help with some JS, Ty.
Post by: Chen Zhen on April 11, 2018, 11:02:18 PM
This will remove the last character from your total variable if it is a decimal/period:

total = total.substring(total.length-1, total.length) == "." ? total.replace(/.$/,"") : total;


.. or perhaps just using regex:

total = total.replace(/\.$/g,"");
Title: Re: Need a quick bit of help with some JS, Ty.
Post by: Chen Zhen on April 15, 2018, 01:00:15 AM

Did that work for you?