0

I have this script in PHP

// To prevent complications using scientific numbers less than 0 ex 7.2341232E-23
if (false !== ($cal = strpos($a, 'E-'))) { $a = 0; }

// Split $a so we only get the numbers before '.'
if (false !== ($cal = strpos($a, '.'))) { $a = substr($a, 0, $cal); }

I would like to know a simple way to make the same function in jQuery.. Any Ideas?

I did what you said... but before I go through and finish the rest of the script could you please tell me if this is right?

var a1 = a.indexOf("E-");
if (a1) { a = 0; }

var a2 = a.indexOf(".");
if (a2) { Math.floor(a); }

EDIT: I have now created a JS Fiddle that contains the script I have created based on your help.. thank you very much.. unfortunately something doesn't seem to work right.. Can Anybody please take a look at it, and tell me what is wrong?

10
  • What have you tried? I think those are quite easy transformation even using vanilla JavaScript. Commented Feb 2, 2013 at 16:30
  • I haven't tried anything, cause I couldn't find anything jQuery relating to strpos. Commented Feb 2, 2013 at 16:31
  • 1
    That's because in vanilla JavaScript there is already a String.indexOf() method. Commented Feb 2, 2013 at 16:33
  • 3
    @PhilipJensBramsted That's because jQuery is mostly a replacement for the DOM. It's not intended as a replacement of all of JavaScript. Commented Feb 2, 2013 at 16:34
  • 3
    I used to say "java != javascript". Now I'll have to start saying "jQuery != javascript". Commented Feb 2, 2013 at 16:36

2 Answers 2

0

A function in my toolkit might help you out,

function strpos (haystack, needle, offset) {
// http://kevin.vanzonneveld.net
// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// +   improved by: Onno Marsman
// +   bugfixed by: Daniel Esteban
// +   improved by: Brett Zamir (http://brett-zamir.me)
// *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
// *     returns 1: 14
var i = (haystack + '').indexOf(needle, (offset || 0));
return i === -1 ? false : i;
}

Or if you want to use jQuery selectors this answer might help you

jQuery or JavaScript equivalent of PHP strpos function to find string on a page

Sign up to request clarification or add additional context in comments.

Comments

0

It is very simple.. just use this:

a = Math.floor(a);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.