/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Number value from pixel representation?

What is the easiest way to separate the leading numbers
from a string of numbers?

For example: In CSS, the width can be in pixels as 200px;
After I get the current value, how can I get the number portion?

I was thinking: maxwidth = PXwidth.match(/d*/);
but this just returns a ‘NaN’

Note: The number portion may range from 0px to 1000px

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@mrhooMar 10.2008 — You can use a match to split a string into an array,

returning you results from size and position properties like

[100,'px'] or [1.25,'em'] or [110,'%']

[CODE]function numstring(str){
var n= '';
var x= str.match(/((.d+)|(d+(.d+)?)) *(w+|%?)/);
if(x){
n= parseFloat(x[1]);
x= x[5];
}
x= x || '';
return [n,x];
}[/CODE]
Copy linkTweet thisAlerts:
@JMRKERauthorMar 10.2008 — Thank you 'mrhoo' ?

I got more answer to my question than I bargained for. ?

I had not even thought of the 'em' and '%' descriptors. :mad:

Appreciate the response. ?
Copy linkTweet thisAlerts:
@mrhooMar 10.2008 — Yeah- firefox will translate most size properties to pixels, no matter how they are set in the source, but that other browser, whatsitsname, is liable to make you work for it...
Copy linkTweet thisAlerts:
@KorMar 10.2008 — if you are using the same measurement units all over, you may simply use the parseInt() method
<i>
</i>var maxwidth = parseInt(PXwidth,10);
×

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,
)...