/    Sign up×
Community /Pin to ProfileBookmark

String to Number prototype

I’m trying to change a string to the sign, numbers and decimal only.

The following only changes the strings to numbers only.
I want to be able to use the numbers in a CSS assignment, like width:200px, without the px if present.

Here are my attempts. Any recommended modifications?

[code]
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″ />

<title> HTML5 page </title>

</head>
<body>

<script type=”text/javascript”>
// Want to leave only sign (-) and/or number (0-9) and/or decimal (.) leaving only signed (optional) integers and decimals.

// String.prototype.toNum = function() { var n = this.replace(/D/g, ”); return n; } // leaves #s only
String.prototype.toNum = function() { var n = this.replace(/D|-|./g, ”); return n; } // leaves #s only too (drops – and .)

alert(’10px’.toNum()+’nn’+’-10px’.toNum()+’nn’+’11.5px’.toNum());
</script>

</body>
</html>
[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@iBeZiFeb 23.2015 — This should work

<i>
</i>String.prototype.toNum = function() { var n = this.replace(/[^d.-]/g, ''); return n; }
Copy linkTweet thisAlerts:
@rootFeb 23.2015 — If your string has one number value in it, you may find parseInt() and parseFloat() of some use.

if its a number value on its own in a string, eg a = "14.02"; then you can simply subtract 0 (zero) from that string to return a number. a -= 0; should do it. I also understand that stringValue * 1; also works

In some circles using RegExp is considered slower, IMHO its what gets the job done.
Copy linkTweet thisAlerts:
@JMRKERauthorFeb 23.2015 — This should work

<i>
</i>String.prototype.toNum = function() { var n = this.replace(/[^d.-]/g, ''); return n; }
[/QUOTE]


That did it. Is the ^ character a 'not' symbol in the regex? I also thought I needed a | character for the 'or' logic.

Thanks!
Copy linkTweet thisAlerts:
@iBeZiFeb 23.2015 — Yep, ^ negates the match. I'm using a character set there instead of using "or" logic, character sets basically match any characters that appear within the square brackets, the ^ at the start of the character set changes it to match anything that [i]doesn't[/i] appear within the square brackets.
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...