I have the following code which gives me result: 4.00 USD
<?php echo $number->currency($auction['Product']['rrp'], $appConfigurations['currency']); ?>
I want to remove decimal. Result should be 4USD, I have tried php function round, but dosn't work. I am using cakephp framework.
Use the options
As indicated in the documentation - you can specify the decimal places to use via the options array:
places Number of decimal places to use. ie. 2
echo $number->currency(
$auction['Product']['rrp'],
$appConfigurations['currency'],
array('places' => 0)
);
Try my code also :
Rs. <?php $offerPrice = $this->Number->currency($relatedProduct['StoreProduct']['offer_price'],"") ;
echo substr($offerPrice,0, strpos($offerPrice,".")) ?>
//input = 3,175.00
//output = 3,175
Related
How do I pass a PHP array to a Nightmare NodeJS script? This is where I'm at:
/*Original array $titles =
Array
(
[0] => title 1 with, a comma after with
[1] => title 2
[2] => title 3
[3] => title 4
)
*/
//PHP
$json_titles = json_encode($titles);
echo $json_titles;
// Output: ["title 1 with, a comma after with","title 2","title 3","title 4"]
// Pass to Nightmare NodeJS script with:
shell_exec('xvfb-run node app.js ' . $json_titles);
//NodeJS app.js:
const getTitles = process . argv[2];
console.log(getTitles)
// Output: [title 1 with, a comma after with,title 2,title 3,title 4]
How do I get the same array in PHP to NodeJS?
As you see below Simon came to the rescue with escapeshellarg. Thanks Simon!
I also ended up having one more step in the Node JS script. I needed to: getTitles = JSON.parse(getTitles);
Change your shell_exec to escape the json like so:
shell_exec('xvfb-run node app.js ' . escapeshellarg($json_titles));
This escapes the double quotes in your JSON so they are passed correctly to node.
You should do this any time you pass variables to the command line, to reduce bugs and security risks.
Edit: as discovered by OP, Node will also have to parse the JSON since it gets the argument as a string. This can be done in the node script as follows:
ParsedTitles = JSON.parse(getTitles);
i received this error when i try to convert drupal 6 module to drupal 7.. i've learned that drupal 7 has a new api in database.. i'am hope someone can give a way to resolve this problem .
db_set_active('data');
$ncoa=db_fetch_array(db_query("SELECT deskripsi FROM {coa} WHERE coaid = '$baris->coa'"));
$ukpengelola=db_fetch_array(db_query("SELECT lokasi FROM {costcenter} WHERE kodecost = '$baris->kodecost'"));
$ukpengguna=db_fetch_array(db_query("SELECT lokasi FROM {costcenter} WHERE kodecost = '$baris->kodecostpemakai'"));
$zstatus=db_fetch_array(db_query("SELECT status_deskripsi AS zket FROM {pengadaan_zstatus} WHERE status_id = '$baris->status_id'"));
db_set_active('default');
db_fetch_array() is depreciated in D7, for a reference how to fix, see http://drupal.org/update/modules/6/7#dbtng
Example - Drupal 6:
<?php
$result = db_fetch_array(db_query("SELECT * FROM {boxes} WHERE bid = %d", $bid));
?>
Drupal 7:
<?php
$result = db_query("SELECT * FROM {block_custom} WHERE bid = :bid", array(':bid' => $bid))->fetchAssoc();
?>
I like to sort my XML response.
This is my code:
// Make some cURL
// Create a simple XML element
$xml = new SimpleXMLElement($resp, LIBXML_NOWARNING, false);
// Output
foreach ($xml->Departure as $departure){
// DEFINE VARIABLES BASED ON XML RESPONSE
$name = $departure['name'];
$rtDate = $departure['rtDate'];
$rtTime = $departure['rtTime'];
$direction = $departure['direction'];
$trainCategory = $departure['trainCategory'];
// CALCULATE DURATION UNTIL NEXT DEPARTURE
$prognosedTime = new DateTime($rtTime);
$currentTime = new DateTime($time);
$interval = $currentTime->diff($prognosedTime);
// OUTPUT FOR BROWSERS
echo $interval->format('%i') . ' Min: ' . $name . ' > ' . $direction . '",';
echo $trainCategory;
echo "<hr/>";
};
?>
Result:
7 Min: Bus 240 > S Ostbahnhof
Bus
-------------------------------------
8 Min: Tram M10 > S+U Warschauer Str.
MetroTram
-------------------------------------
2 Min: U1 > Uhlandstr.
U-Bahn
-------------------------------------
0 Min: Tram M10 > S+U Hauptbahnhof
MetroTram
Problem:
My result should be sorted by $interval
I read
PHP sorting issue with simpleXML several times but I don't get it. So I wanted a shorter solution (for bloody beginners) and found something nice in Sort Foreach Loop after ID. But then I need arrays. Another solution is very close to that and shows how to define arrays: ASC sort foreach. But here I have no idea how to put all my data into an array as I never know how many rows the response will have. I believe I am very close to a solution but don't get it since 2 days. narf
I do not understand the role of qw in Perl, which I understood in in debugging the code in this thread.
Its manual is very limited here: qw/string/.
I am trying to understand what goes wrong in the following code I am doing simple manipulation to the data (multiplying by one (1.0)).
Original code which is a simplified version of the situation in a real-world case
use v5.16;
use Math::Geometry::Planar qw(SegmentLineIntersection);
sub x_intercepts {
my ($points) = #_;
die 'Must pass at least 2 points' unless #$points >= 2;
my #intercepts;
my #x_axis = ( [0, 2000], [1, 2000] );
foreach my $i (0 .. $#$points - 1) {
my $intersect = SegmentLineIntersection([#$points[$i,$i+1], #x_axis]);
push #intercepts, $intersect if $intersect;
}
return \#intercepts;
}
my #xs = qw/22.595451 20.089094 17.380813 15.091260 12.477935 10.054821 7.270003 4.804673 4.728526 4.619254 4.526920 4.418416 4.321419 4.219890 4.123336 4.009777 3.912648 3.804183 3.705847 3.597756 3.512301 3.393413 3.301963 3.196725 3.098560 3.007482 2.899825 2.801002 2.688680 2.598862 2.496139 2.393526 2.282183 2.190449 2.084530 1.987778 1.877562 1.788788 1.678473 1.578123 1.467071 1.373372 1.283629 1.176670 1.071805 0.975422 0.877622 0.767820 0.667409 0.562480 0.469068 0.354589 0.264291 0.152522 0.063765 -0.045323 -0.136783 -0.248559 -0.343694 -0.459178 -0.551917 -0.640803 -0.755502 -0.845535 -0.955227 -1.045879 -1.155600 -1.254556 -1.365163 -1.461669 -1.571370 -1.658043 -1.772672 -1.865942 -1.981037 -2.073702 -2.176205 -2.276184 -2.367371 -2.476278 -2.567385 -2.686326 -2.777433 -2.884357 -2.980067 -3.087754 -3.183607 -3.291003 -3.386942 -3.495822 -3.586759 -3.702955 -3.793550 -3.900680 -3.999672 -4.093094 -4.200047 -4.301026 -4.399742 -4.493190 -4.602013 -4.705124 -4.812383 -4.907510 -5.022904 -5.109829 -5.214304 -5.317662 -7.703172 -10.350131 -12.921361 -15.431203 -18.188013 -20.544248 -22.822808 -25.679854 -22.999092 -20.540434 -17.964916 -15.398857 -12.990375 -10.402209 -7.888263 -5.504909 -5.217892 -5.109841 -5.014187 -4.908558 -4.811807 -4.704282 -4.605555 -4.504613 -4.406546 -4.292540 -4.204043 -4.088770 -3.995524 -3.905669 -3.796544 -3.707958 -3.596449 -3.490966 -3.382869 -3.293054 -3.185821 -3.088417 -2.971214 -2.880314 -2.772518 -2.677986 -2.569794 -2.473668 -2.365096 -2.276422 -2.179232 -2.068195 -1.973110 -1.859565 -1.771873 -1.669422 -1.569953 -1.462626 -1.364911 -1.258100 -1.159318 -1.050486 -0.959979 -0.849149 -0.749421 -0.640950 -0.547122 -0.451754 -0.344463 -0.252269 -0.134625 -0.051640 0.052970 0.154112 0.266505 0.353926 0.468739 0.561666 0.673810 0.759169 0.881697 0.973207 1.082409 1.170424 1.282163 1.378096 1.472728 1.586454 1.678473 1.785688 1.873862 1.984090 2.086021 2.196799 2.292400 2.386097 2.493190 2.601726 2.694346 2.803450 2.901878 3.011959 3.103050 3.196979 3.294507 3.397563 3.504076 3.600163 3.712539 3.809573 3.919799 4.012314 4.120694 4.216406 4.322895 4.416466 4.522871 4.623917 4.735925 4.826929 7.361253 9.647208 12.337984 14.870260 17.439730 19.921717 22.524080 25.125903/;
# -1 1.0 3 4 5 6 7 7 8 9 10/;
my #ys = qw/3699.316162 3676.939697 3659.950195 3641.476318 3605.965576 3580.152588 3555.092529 3528.118408 3509.595703 3504.416504 3508.355957 3510.452881 3510.304443 3499.548340 3499.943848 3493.196533 3499.488770 3494.984375 3494.390137 3493.938965 3492.606689 3493.434814 3488.926514 3488.890869 3484.599854 3488.077393 3484.177979 3477.801758 3478.098877 3472.040039 3477.642090 3477.802002 3472.908447 3478.532715 3469.805420 3472.759766 3464.269043 3463.950684 3465.888184 3458.441650 3459.006104 3455.686035 3455.677490 3454.548828 3454.241211 3455.250000 3449.803711 3447.423340 3457.498779 3448.445557 3453.106689 3447.701172 3444.543945 3448.558350 3450.073730 3449.884033 3444.751953 3444.056152 3444.825195 3449.671143 2593.656494 788.985779 776.407776 776.385925 767.522522 774.794250 770.596008 775.218384 770.962769 766.214294 766.735962 759.847351 760.073486 760.026489 753.721741 755.601929 753.942566 758.356506 747.932617 746.332214 746.464844 747.055115 750.173706 737.463379 739.810486 742.011475 744.332581 743.022461 737.302490 737.396606 734.325256 737.305359 740.642395 734.709717 735.754089 737.961182 740.697510 727.310913 730.918640 728.325012 721.845459 728.389893 727.765625 729.961243 725.608459 723.581909 730.736084 720.707764 720.398193 691.499390 657.534546 628.854431 615.219727 572.711365 561.505127 539.865173 517.139709 545.076416 580.880005 602.851135 628.006104 657.119263 683.746033 692.044373 716.640320 715.451294 715.415405 718.968018 723.698669 729.758606 728.564514 734.523376 731.454468 736.899780 731.257263 729.390686 732.837463 730.479431 733.497803 735.362732 742.581543 742.998108 735.918579 738.190002 738.108337 738.154297 740.425537 739.496033 743.105835 737.412537 740.537354 747.498108 747.424194 748.992920 752.244263 755.603455 756.611755 761.916504 762.920715 752.261658 758.293823 755.664062 753.728882 756.746338 754.825684 755.080444 761.192383 761.955505 763.209351 760.402771 764.342224 775.400940 767.148621 775.184998 777.084595 778.357117 776.217163 1086.248291 3444.476807 3438.105957 3440.625000 3438.325195 3438.430420 3449.251709 3453.216309 3452.126465 3458.547119 3451.694336 3456.417725 3457.336426 3457.777832 3455.553955 3457.256348 3458.823486 3459.088623 3459.492187 3463.538818 3466.455078 3456.521240 3459.809082 3457.505127 3462.721191 3466.518066 3467.562744 3469.211182 3469.120361 3464.043945 3466.291992 3472.698486 3476.146729 3471.635254 3472.539551 3475.163574 3473.687744 3479.102051 3488.351807 3482.367432 3481.961914 3484.844238 3481.511719 3482.469238 3488.947021 3488.882080 3491.247314 3499.116699 3511.889893 3539.602783 3565.981445 3598.203613 3628.028076 3657.928955 3685.231689/;
# my #ys = 1.0 * #ys;
my #input_list ;
foreach my $i ( 0..$#ys ) {
push #input_list, [ $xs[$i], $ys[$i] ] ;
}
my $intercept_list = x_intercepts(\#input_list) ;
say join ",", #$_ for #$intercept_list ;
where I just activate this line # my #ys = 1.0 * #ys; by commenting out which gives
Must pass at least 2 points at test2.pl line 7.
which suggests that 1.0 * qw_stuff is doing something else than I expect.
I run
my #ys = 1 * scalar(#ys); my #ys = qw/#ys/;
but the same error persists.
How can you do the simple manipulation of the data between integer and qw in Perl?
Quote Words:
my #xs = qw/22.595451 20.089094 17.380813 15.091260 12.477935 10.054821 7.270003 4.804673 4.728526 4.619254 4.526920 4.418416 4.321419 4.219890 4.123336 4.009777 3.912648 3.804183 3.705847 3.597756 3.512301 3.393413 3.301963 3.196725 3.098560 3.007482 2.899825 2.801002 2.688680 2.598862 2.496139 2.393526 2.282183 2.190449 2.084530 1.987778 1.877562 1.788788 1.678473 1.578123 1.467071 1.373372 1.283629 1.176670 1.071805 0.975422 0.877622 0.767820 0.667409 0.562480 0.469068 0.354589 0.264291 0.152522 0.063765 -0.045323 -0.136783 -0.248559 -0.343694 -0.459178 -0.551917 -0.640803 -0.755502 -0.845535 -0.955227 -1.045879 -1.155600 -1.254556 -1.365163 -1.461669 -1.571370 -1.658043 -1.772672 -1.865942 -1.981037 -2.073702 -2.176205 -2.276184 -2.367371 -2.476278 -2.567385 -2.686326 -2.777433 -2.884357 -2.980067 -3.087754 -3.183607 -3.291003 -3.386942 -3.495822 -3.586759 -3.702955 -3.793550 -3.900680 -3.999672 -4.093094 -4.200047 -4.301026 -4.399742 -4.493190 -4.602013 -4.705124 -4.812383 -4.907510 -5.022904 -5.109829 -5.214304 -5.317662 -7.703172 -10.350131 -12.921361 -15.431203 -18.188013 -20.544248 -22.822808 -25.679854 -22.999092 -20.540434 -17.964916 -15.398857 -12.990375 -10.402209 -7.888263 -5.504909 -5.217892 -5.109841 -5.014187 -4.908558 -4.811807 -4.704282 -4.605555 -4.504613 -4.406546 -4.292540 -4.204043 -4.088770 -3.995524 -3.905669 -3.796544 -3.707958 -3.596449 -3.490966 -3.382869 -3.293054 -3.185821 -3.088417 -2.971214 -2.880314 -2.772518 -2.677986 -2.569794 -2.473668 -2.365096 -2.276422 -2.179232 -2.068195 -1.973110 -1.859565 -1.771873 -1.669422 -1.569953 -1.462626 -1.364911 -1.258100 -1.159318 -1.050486 -0.959979 -0.849149 -0.749421 -0.640950 -0.547122 -0.451754 -0.344463 -0.252269 -0.134625 -0.051640 0.052970 0.154112 0.266505 0.353926 0.468739 0.561666 0.673810 0.759169 0.881697 0.973207 1.082409 1.170424 1.282163 1.378096 1.472728 1.586454 1.678473 1.785688 1.873862 1.984090 2.086021 2.196799 2.292400 2.386097 2.493190 2.601726 2.694346 2.803450 2.901878 3.011959 3.103050 3.196979 3.294507 3.397563 3.504076 3.600163 3.712539 3.809573 3.919799 4.012314 4.120694 4.216406 4.322895 4.416466 4.522871 4.623917 4.735925 4.826929 7.361253 9.647208 12.337984 14.870260 17.439730 19.921717 22.524080 25.125903/;
is identical to
my #xs = ('22.595451', '20.089094', '17.380813', '15.091260', 'etc...');
but, as you can see saves a bunch of typing.
So now that you've got a bunch of strings, you can just use them as numbers and perl will automatically do the conversion for you (generally recommended), or if their stringatude offends you, you can explicitly do a premature manual conversion.
When you want to transform an array or list, think map or foreach.
#ys = map { 1 * $_ } #ys;
or
foreach my $element (#ys) {
$element *= 1; # or += 0 both work.
}
... but seriously, what is that actually buying you? If you allow Perl to do the conversion at it's leisure, the values will become dual valued and won't need to be re-converted if you use them as strings. With Manual conversion you risk ending up with a numeric only value that may require re-stringifying and thus a possible second bite at the round-off error apple.
The only time you may really want to do a manual conversion is when you need to convert to BigInt, BigFloat, or similar objects (i.e. you are concerned about any possible loss of precision an auto convert to a float will do); or if you are prepping the values for output via JSON or YAML to a system that cares about the difference between 'text representation of a number' (49) and 'quoted strings of digits' ("49"), though be warned that this counts as re-stringifying.
I am trying to extract the Interface from an array created from an SNMP Query.
I want to create an array like THIS:
my #array = ( "Gig 11/8",
"Gig 10/1",
"Gig 10/4",
"Gig 10/2");
It currently looks like THIS:
my #array =
( "orem-g13ap-01 Gig 11/8 166 T AIR-LAP11 Gig 0",
"orem-g15ap-06 Gig 10/1 127 T AIR-LAP11 Gig 0",
"orem-g15ap-05 Gig 10/4 168 T AIR-LAP11 Gig 0",
"orem-g13ap-03 Gig 10/2 132 T AIR-LAP11 Gig 0");>
I am doing THIS:
foreach $ints (#array) {
#gig = substr("$ints", 17, 9);
print("Interface: #gig");
Sure it works, but the hostname [orem-g15ap-01] doesn't always stay the same length, it varies depending on the site. I need to extract the word "Gig" plus the next 6 characters. I have no idea what is the best way of doing this.
I am a novice at perl but trying. Thanks
# "I need to extract the word "Gig" plus the next 6 characters."
# This looks like a fixed-with file format, so consider using unpack.
foreach ( #lines ) {
my( $orem, $gig, $rest ) = unpack 'a17 a9 a*';
print "[$gig]\n";
}
If it's not fixed-with format, then you need to find out what the file spec is and then maybe use a regular expression, something like:
my( $orem, $gig, $rest ) = m/(\S+)\s+(.{9})(.*)/;
But this will not work in the general case without a proper file spec.
Stuff like that is what Perl is made for. Regular Expressions are the way to go. Read the perldoc perlre.
foreach $ints (#array) {
$ints =~ s/(Gig.{6})/$1/g;
}
So you want the second and third field.
my #array = map { /^\S+\s+(\S+\s\S+)/s } #source;
This one is like ikegami's, but I recommend that if you know how something you want looks, then by all means, specify that. Because this is done in a list context, any string that does not match the spec, returns an empty list--or is ignored.
my #results = map { m!(\bGig\s+\d+/d+)! } #array;