News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Need a quick bit of help with some JS, Ty.

Started by MoreBloodWine, March 30, 2018, 04:19:19 PM

Previous topic - Next topic

MoreBloodWine

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 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>

Want a sig like mine for your BTCGuild stats ? Then check this out: Spend-ur-Bits


MoreBloodWine

Seem to have fixed it by changing the 4 to  0.
Want a sig like mine for your BTCGuild stats ? Then check this out: Spend-ur-Bits


MoreBloodWine

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.
Want a sig like mine for your BTCGuild stats ? Then check this out: Spend-ur-Bits


MoreBloodWine

? Anyone ?

No rush, just trying not to forget about it lol
Want a sig like mine for your BTCGuild stats ? Then check this out: Spend-ur-Bits


Chen Zhen

#4
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(/.$/,"");

My SMF Mods & Plug-Ins

WebDev

"Either you repeat the same conventional doctrines everybody is saying, or else you say something true, and it will sound like it's from Neptune." - Noam Chomsky

MoreBloodWine

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
Want a sig like mine for your BTCGuild stats ? Then check this out: Spend-ur-Bits


Chen Zhen

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,"");

My SMF Mods & Plug-Ins

WebDev

"Either you repeat the same conventional doctrines everybody is saying, or else you say something true, and it will sound like it's from Neptune." - Noam Chomsky

Chen Zhen


My SMF Mods & Plug-Ins

WebDev

"Either you repeat the same conventional doctrines everybody is saying, or else you say something true, and it will sound like it's from Neptune." - Noam Chomsky

Advertisement: