How do I preg_match this? - preg-match

I need help to write a preg_match to extract the 0.85 and 1.00 from this into php variables. Been trying all day with no success!
Chop Suey</a></td><td align="right">0.85</td><td align="right">1.00</td>

This will give you 0.85 and 1.00 from your specified string, stored in $values[1] and $values[2] respectively.
$values = array();
preg_match('/Chop Suey<\/a><\/td><td align="right">([\d]+\.[\d]+)<\/td><td align="right">([\d]+\.[\d]+)<\/td>/', 'Chop Suey</a></td><td align="right">0.85</td><td align="right">1.00</td>', $values);

You could also be more dynamic with it. Instead of statically looking for "chop suey" why not look for other alignments.
Here is a sample to that. (very basic).
preg_match("/\d+.\d+/",$content,$output);
(above match, would give you all the decimals you need in correct order.)
$output[0] (is the array you can loop)
for the exact numbers above, you'd use $output[0][0] and $output[0][1]
as seen in the regex example here

Related

How to format a number with XPath in Tibco BW 5

I've managed to format the following lines in XPath, from this format:
1000.50
30
to this:
100050
3000
The solution I've adopted is:
concat(substring-before([number], '.'), substring-after([number], '.'))
If the . is not present I directly multiply the number by 100.
I'm wondering if there is any better way to do that. My second thought was using Java.
What goes wrong if you just multiply by 100? So long as the result of multiplying by 100 is an exact integer, it should be formatted without a "." when converted to a string. If there are rounding errors that mean the result is not an exact integer, you might want to use round().
The concat() approach seems fragile to me: what if someone gives you input like 1000.5 or perhaps 1000.500?

rounding a number in ruby

I would like to round this figure to the nearest whole number. I am generating an xml based on an excel file, and would like to round the figure.
Here is my code:
xml.POS110 “wert”: “#{row[18]}”
I have tried:
xml.POS110 “wert”: “#{row[18]}”.round(0)
Move the round inside the quotes. Before, you were just trying to round a string. Also the default argument for round is 0 so you don't need to specify it (but you can if you really want to).
“#{row[18].round(0)}"

Smarty Float format thousands without php?

So, I'm trying to make smarty format a number like 1000.66 to 1,000.66 but when I use {$number|number_format:0:'.':','} it just rounds it up to 1,001...
I've googled but I can't find anything about it...
|number_format just uses the php function of the same name. Your problem is that you specified the number of decimals as 0, and the function rounds up the number provided. Try with
{$number|number_format:2:'.':','}

How can I combine comma format with scientific format in SAS?

I have data that I would like to represent as comma10.2 when less than 1,000,000 and e10. when greater than or equal to 1,000,000. It seems like there might be a way to do this using the picture format, so I thought I might also making missing values show up as --. This is what I've got so far:
proc format;
picture DashMiss . = '--' (noedit)
low - <1000000 = "000,009.99"
1000000 - high = ????;
run;
I'm not sure how to represent scientific notation using picture (hence the question marks). I don't have to just use picture if there's an easier way to do it.
I figured out how to use brackets to add the conditional format:
proc format;
picture DashMiss . = '--' (noedit)
low - <1000000 = "000,009.99"
1000000 - high = [e10.];
run;
I believe you could've simply used the best6. format or bestd6.2 to achieve the same results. It naturally uses scientific notation whenever the length is beyond the first of the 2 integers.

Convert string with comma to integer

Is there any neat method to convert "1,112" to integer 1112, instead of 1?
I've got one, but not neat:
"1,112".split(',').join.to_i #=> 1112
How about this?
"1,112".delete(',').to_i
You may also want to make sure that your code localizes correctly, or make sure the users are used to the "international" notation. For example, "1,112" actually means different numbers across different countries. In Germany it means the number a little over one, instead of one thousand and something.
Corresponding Wikipedia article is at http://en.wikipedia.org/wiki/Decimal_mark. It seems to be poorly written at this time though. For example as a Chinese I'm not sure where does these description about thousand separator in China come from.
Some more convenient
"1,1200.00".gsub(/[^0-9]/,'')
it makes "1 200 200" work properly aswell
The following is another method that will work, although as with some of the other methods it will strip decimal places.
a = 1,112
b = a.scan(/\d+/).join().to_i => 1112
I would do using String#tr :
"1,112".tr(',','').to_i # => 1112
If someone is looking to sub out more than a comma I'm a fan of:
"1,200".chars.grep(/\d/).join.to_i
dunno about performance but, it is more flexible than a gsub, ie:
"1-200".chars.grep(/\d/).join.to_i
String count = count.replace(",", "");

Resources