javascript - find range value between two floats
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?
From 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.com/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 comment 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 ;)
精彩评论