admin管理员组文章数量:1323714
I have a string like so:
(999.08) - (1025.67)
I need to be able to find the range between these two values as a floting point even if they are negative values so (-999.08) - (-1025.67) would also be in scope.
Assume i would need to use a regex and join the two in some sort of array?
I have a string like so:
(999.08) - (1025.67)
I need to be able to find the range between these two values as a floting point even if they are negative values so (-999.08) - (-1025.67) would also be in scope.
Assume i would need to use a regex and join the two in some sort of array?
Share Improve this question asked Oct 19, 2010 at 13:48 RyanP13RyanP13 7,75328 gold badges102 silver badges171 bronze badges 1- 1 it seems you have to create array using 'for' loop only. Can't imagine other solution. – Danil Commented Oct 19, 2010 at 13:51
6 Answers
Reset to default 3From looking over everyone else's answers, I can't tell if any of them deal with negative numbers properly. I'll throw this in the ring in case anyone needs it.
function parse(str) {
// init to NaN
var result = Number.NaN;
// capture numbers in groups
var pat = new RegExp(/\((-?\d+.?\d*)\)\s*-\s*\((-?\d+.?\d*)\)/);
var match = str.match(pat);
if (match) {
// match[0] is whole match which is not useful
var a = new Number(match[1]); // 1st group is 1st number
var b = new Number(match[2]); // 2nd group is 2nd number
result = Math.abs(a - b);
}
return result;
}
demo: http://jsbin./oseba4/edit
You can split the string on the " - " using .split, use replace to remove the brackets, and then parseFloat the two numbers. After that, check for the highest of the two numbers, and subtract for the range.
There are a couple of ways: Here is one: sort of the long way around
var myString = "(999.08) - (1025.67)"
var myFloatValues = mystring.split("-");
myFloatValues[0] = myFloatValues[0].replace("(", "").replace(")", "");
myFloatValues[1] = myFloatValues[1].replace("(", "").replace(")", "");
myFloatValues[0] = parseFloat(myFloatValues[0])
myFloatValues[1] = parseFloat(myFloatValues[1])
Here is using a regex:
var myString = "(999.08) - (1025.67)"
var myFloatValues = (myString.replace(/\(*|\)*|\s*/g, "")).split("-");
myFloatValues[0] = parseFloat(myFloatValues[0])
myFloatValues[1] = parseFloat(myFloatValues[1])
Note: a useful site I have been using for a ling time
This site helps a person generate valid regular Expressions. Give it a try http://www.jslab.dk/tools.regex.php
in addition to John hartsocks answer: add a check after you set the float values to swap around the values if the first value is bigger than the second
function Check(myValue,myString)
{
var myFloatValues = (mystring.replace(/\(*|\)*|\s*/g, "")).split("-");
myFloatValues[0] = parseFloat(myFloatValues[0]);
myFloatValues[1] = parseFloat(myFloatValues[1]);
if (myFloatValues[0] > myFloatValues[1]) // swap
{
var temp = myFloatValues[0];
myFloatValues[0] = myFloatValues[1];
myFloatValues[1] = temp;
}
if (myFloatValues[0] < myValue && myFloatValues[1] > myValue) // this will be a problem if you dont swap them
return true;
return false;
}
Pesuo code:
// Our variables
float num1;
float num2;
string myString = "(999.08) - (1025.67)";
// Split the data string on - character
arrParts[] = myString.split("-");
// Loop through each resulting split
for int i = 0; i < arrParts.Count; i++)
{
// Trim result to remove whitespace
arrParts[i] = arrParts[i].trim();
// Take all the characters in string except first and last
arrParts[i] = arrParts[i].substring(1, part.length-2);
}
// Cast out numbers
num1 = (float)arrParts[0];
num2 = (float)arrParts[1];
Solution assumptions
Assumes input string is correct format, and that no fewer or more than 2 valid float numbers will be provided.
For range calculation:
Two ways, either subtract either from either and get absolute value to determine range, or take the lengthier method of guaranteeing smaller number is taken for bigger
Notes on regexp
I would argue against using regexp where possible (although this is very subjective) because it can turn reviewing this code in the future into a difficult task.
If you do use regexp make sure you ment in the expected input formats to protect against this.
This is where eval
* shows its true power.
range = Math.abs(eval(string));
*No, it's NOT evil ;)
本文标签: regexjavascriptfind range value between two floatsStack Overflow
版权声明:本文标题:regex - javascript - find range value between two floats - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742127463a2422006.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论