Advertisement:

Author Topic: Need a quick bit of help with some JS, Ty.  (Read 2986 times)

Offline MoreBloodWine

  • SMF Hero
  • ******
  • Posts: 2,723
  • Gender: Male
  • ...
    • Uncharted 2: Among Thieves Stat Sig Generator
Need a quick bit of help with some JS, Ty.
« 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 for what I'm talking about. Subset of line 3.

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

Code: [Select]
<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


Offline MoreBloodWine

  • SMF Hero
  • ******
  • Posts: 2,723
  • Gender: Male
  • ...
    • Uncharted 2: Among Thieves Stat Sig Generator
Re: Need a quick bit of help with some JS, Ty.
« Reply #1 on: March 30, 2018, 05:45:20 PM »
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


Offline MoreBloodWine

  • SMF Hero
  • ******
  • Posts: 2,723
  • Gender: Male
  • ...
    • Uncharted 2: Among Thieves Stat Sig Generator
Re: Need a quick bit of help with some JS, Ty.
« Reply #2 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.
Want a sig like mine for your BTCGuild stats ? Then check this out: Spend-ur-Bits


Offline MoreBloodWine

  • SMF Hero
  • ******
  • Posts: 2,723
  • Gender: Male
  • ...
    • Uncharted 2: Among Thieves Stat Sig Generator
Re: Need a quick bit of help with some JS, Ty.
« Reply #3 on: April 11, 2018, 05:21:09 PM »
? 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


Offline Chen Zhen

  • Sophist Member
  • *****
  • Posts: 1,177
  • Gender: Male
  • If you're going through hell, keep going!
    • Underdog-01 on GitHub
    • WebDev.ca
Re: Need a quick bit of help with some JS, Ty.
« Reply #4 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:

Code: [Select]
Math.round()
Math.floor()
Math.trunc()

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

Code: [Select]
total.replace(/.$/,"");
« Last Edit: April 11, 2018, 06:11:03 PM by Chen Zhen »

My SMF Mods & Plug-Ins

WebDev
Simple Portal Support

Request politely & demand nothing.

Offline MoreBloodWine

  • SMF Hero
  • ******
  • Posts: 2,723
  • Gender: Male
  • ...
    • Uncharted 2: Among Thieves Stat Sig Generator
Re: Need a quick bit of help with some JS, Ty.
« Reply #5 on: April 11, 2018, 06:26: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:

Code: [Select]
Math.round()
Math.floor()
Math.trunc()

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

Code: [Select]
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


Offline Chen Zhen

  • Sophist Member
  • *****
  • Posts: 1,177
  • Gender: Male
  • If you're going through hell, keep going!
    • Underdog-01 on GitHub
    • WebDev.ca
Re: Need a quick bit of help with some JS, Ty.
« Reply #6 on: April 11, 2018, 11:02:18 PM »
This will remove the last character from your total variable if it is a decimal/period:
Code: [Select]
total = total.substring(total.length-1, total.length) == "." ? total.replace(/.$/,"") : total;

.. or perhaps just using regex:
Code: [Select]
total = total.replace(/\.$/g,"");

My SMF Mods & Plug-Ins

WebDev
Simple Portal Support

Request politely & demand nothing.

Offline Chen Zhen

  • Sophist Member
  • *****
  • Posts: 1,177
  • Gender: Male
  • If you're going through hell, keep going!
    • Underdog-01 on GitHub
    • WebDev.ca
Re: Need a quick bit of help with some JS, Ty.
« Reply #7 on: April 15, 2018, 01:00:15 AM »

Did that work for you?

My SMF Mods & Plug-Ins

WebDev
Simple Portal Support

Request politely & demand nothing.