1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Rounding Rules

Discussion in 'JavaScript' started by clay5366, Feb 14, 2018.

  1. #1
    I have a situation where I have rounding rules to apply the the results of 2 fields based on the results value.
    >10 rounded to the nearest integer. No problem there.
    The other 2 are: <=10 >5 is rounded to the nearest .5 and >5 rounded to the nearest .1
    I understand Switch and case, just trying to work out the math for the rounding
    SEMrush
    Thanks for the assist
    David
     
    clay5366, Feb 14, 2018 IP
    SEMrush
  2. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,607
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #2
    Round to .1 decimal:
    var rounded =Math.round(number *10)/10;

    Round to integer:
    var rounded = Math.round(number);

    Round to .5 decimal
    - will require an if else statement if > .5 or >= .5
     
    ccoonen, Feb 14, 2018 IP
  3. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,607
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #3
    Whoops, I meant > .5 or <= .5
     
    ccoonen, Feb 14, 2018 IP
  4. clay5366

    clay5366 Member

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #4
    Thank you much. Been messing around with the math. Working well.
    I am trying to get my head around switch. Seems straight forward, but no output to the field 'gd_TTTmm_rnd.value'
    The values output by themselves until I added the switch

    var tttmm = ((.2*(((.025*pd)+(.3*mod))+19))*sqrrtexp)

    switch (tttmm)
    {
    case (tttmm > 10):
    gd_TTTmm_rnd.value = Math.round(tttmm);
    break;
    case (tttmm < 10 && tttmm > 5):
    gd_TTTmm_rnd.value = Math.round((tttmm*10)/10);
    break;
    }
     
    clay5366, Feb 15, 2018 IP