0

How to get last number with jquery? For example 25.50 how to get last 50? How to get first number too? for example 25 enter code here

Thanks for help. Happy coding

For example lets say I have a variable and its 25.50 ok? I need to get 50 and if 50 =< 50 make it 25 I mean 25.00 if 50 > 50 make it +1 I mean 26

  • use split function for ex. var data = input.split('.'); var a = data [0]; // 25 var b = data [1]; 50 – Ravi Ashara Mar 09 '21 at 12:30
  • 1
    So you want to [round](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round) the number? – freedomn-m Mar 09 '21 at 12:39
  • 1
    Seems like you're searching for [`Math.round()`](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Math/round). – NullDev Mar 09 '21 at 12:39
  • @NullDev Yes dear but I make 50 too less –  Mar 09 '21 at 12:42
  • I mean math.round make 50 more but I need 50 too make less 51 ets more –  Mar 09 '21 at 12:47
  • 1
    @Mychannel See my answer. – NullDev Mar 09 '21 at 13:00
  • 1
    Does this answer your question? [Get decimal portion of a number with JavaScript](https://stackoverflow.com/questions/4512306/get-decimal-portion-of-a-number-with-javascript) – Nikolay Shebanov Mar 09 '21 at 21:18

1 Answers1

1

First off: You don't need jQuery for that.

If I understand correctly you want to round the number, but make it round down on <= .5 and up on > .5. This can be done by negating both Math.round as well as the input:

let round = num => (-Math.round(-num));

console.log(round(25.50))
console.log(round(12.60));
console.log(round(9.40));

This is possible, because as standardized in the spec, Math.round() rounds towards positive infinity.

  1. Return the integral Number closest to n, preferring the Number closer to +∞ in the case of a tie.
NullDev
  • 6,739
  • 4
  • 30
  • 54