Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP
If you divide £10,000 between 17 lucky children, each gets £588.23 ... but if you divide the number 10,000 by the number 17, you get 588.235294117647. How do you format the data to the appropriate number of decimal places?
In Ruby, you can use the
% operator on a string - which is an alias for the
sprintf function. Here's an example, showing each of the two ways embedded in a
puts statement:
puts "With #{'%-2d' % infavour} kids, each gets #{'%8.2f' % amount}"
puts "With #{sprintf '%-2d',infavour} kids, each gets #{sprintf '%8.2f',amount}"
Full exaample - including a list of the more common formatting letters (the "d" and "f" in my example) -
[here].
Ruby has plenty of variety, There's a further example of an almost identical application written on an earlier course -
[here]. And in that I used the same % operator in a rather different way:
print "With %-2d in favour (add %10.10s) each gets %06.2f\n" %
[nif, names[currentname],howm]
Co-incidentally, my email last night included a similar question about PHP. My answer:
Sample PHP code that prints out number to full places, then just to 2:
 <?php
$var = 296.531136;
print ("$var\n");
printf ("%.2f\n",$var);
?>
Runs as follows:
wizard:sep11 graham$ php phpdemo
296.531136
296.53
wizard:sep11 graham$
You'll find PHP examples in various places in our examples directory including
[here] and
[here].
In Perl and C, you have
printf ... and in Python you have the
% operator although there's also something new as well ... via
str.format. See
[here].
(written 2011-09-08)
28ec
Associated topics are indexed under
R109 - Ruby - Strings and Regular Expressions [3758] Ruby - standard operators are overloaded. Perl - they are not - (2012-06-09)
[3757] Ruby - a teaching example showing many of the language features in short but useful program - (2012-06-09)
[3621] Matching regular expressions, and substitutions, in Ruby - (2012-02-23)
[2980] Ruby - examples of regular expressions, inheritance and polymorphism - (2010-10-02)
[2623] Object Oriented Ruby - new examples - (2010-02-03)
[2621] Ruby collections and strings - some new examples - (2010-02-03)
[2614] Neatly formatting results into a table - (2010-02-01)
[2608] Search and replace in Ruby - Ruby Regular Expressions - (2010-01-31)
[2295] The dog is not in trouble - (2009-07-17)
[2293] Regular Expressions in Ruby - (2009-07-16)
[1891] Ruby to access web services - (2008-11-16)
[1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
[1875] What are exceptions - Python based answer - (2008-11-08)
[1588] String interpretation in Ruby - (2008-03-21)
[1305] Regular expressions made easy - building from components - (2007-08-16)
[1195] Regular Express Primer - (2007-05-20)
[987] Ruby v Perl - interpollating variables - (2006-12-15)
[986] puts - opposite of chomp in Ruby - (2006-12-15)
[970] String duplication - x in Perl, * in Python and Ruby - (2006-12-07)
H109 - PHP - Input / Output [3159] Returning multiple values from a function call in various languages - a comparison - (2011-02-06)
[3029] PHP data sources - other web servers, large data flows, and the client (browser) - (2010-11-04)
[2964] An introduction to file handling in programs - buffering, standard in and out, and file handles - (2010-09-21)
[1780] Server overloading - turns out to be feof in PHP - (2008-09-01)
[1442] Reading a file multiple times - file pointers - (2007-11-23)
[1113] File and URL reading in PHP - (2007-03-20)
[1096] Sample script - FTP to get a file from within PHP - (2007-03-01)
[1094] PHP fread - truncated data - (2007-02-27)
[997] Most recent file in a directory - PHP - (2006-12-18)
[709] Handling huge data files in PHP - (2006-05-04)
[653] Easy feed! - (2006-03-21)
[616] printf - a flawed but useful function - (2006-02-22)
[114] Relative or absolute milkman - (2004-11-10)
H107 - String Handling in PHP [4072] Splitting the difference with PHP - (2013-04-27)
[4071] Setting up strings in PHP - (2013-04-27)
[3790] Solution looking for a problem? Lookahead and Lookbehind - (2012-06-30)
[3789] More than just matching with a regular expression in PHP - (2012-06-30)
[3788] Getting more than a yes / no answer from a regular expression pattern match - (2012-06-30)
[3534] Learning to program in PHP - Regular Expression and Associative Array examples - (2011-12-01)
[3516] Regular Expression modifiers in PHP - summary table - (2011-11-12)
[3515] PHP - moving from ereg to preg for regular expressions - (2011-11-11)
[3020] Handling (expanding) tabs in PHP - (2010-10-29)
[2629] Curly braces within double quoted strings in PHP - (2010-02-09)
[2238] Handling nasty characters - Perl, PHP, Python, Tcl, Lua - (2009-06-14)
[2165] Making Regular Expressions easy to read and maintain - (2009-05-10)
[2046] Finding variations on a surname - (2009-02-17)
[1799] Regular Expressions in PHP - (2008-09-16)
[1613] Regular expression for 6 digits OR 25 digits - (2008-04-16)
[1603] Do not SHOUT and do not whisper - (2008-04-06)
[1533] Short and sweet and sticky - PHP form input - (2008-02-06)
[1372] A taster PHP expression ... - (2007-09-30)
[1336] Ignore case in Regular Expression - (2007-09-08)
[1058] PHP Regular expression to extrtact link and text - (2007-01-31)
[1008] Date conversion - PHP - (2006-12-26)
[728] Looking ahead and behind in a Regular Expression - (2006-05-22)
[716] Evaluating arithmetic expressions in configuration files - (2006-05-10)
[642] How similar are two words - (2006-03-11)
[608] Don't expose your regular expressions - (2006-02-15)
[589] Robust PHP user inputs - (2006-02-03)
[574] PHP - dividing a string up into pieces - (2006-01-23)
[560] The fencepost problem - (2006-01-10)
[558] Converting between acres and hectares - (2006-01-08)
[493] Running a Perl script within a PHP page - (2005-11-12)
[463] Splitting the difference - (2005-10-13)
[422] PHP Magic Quotes - (2005-08-22)
[337] the array returned by preg_match_all - (2005-06-06)
[54] PHP and natural sorting - (2004-09-19)
[31] Here documents - (2004-08-28)
55f4
Some other Articles
How many days to Christmas?What is on the Melksham Agenda?Automed web site testing scripted in Ruby using watir-webdriverOur National Autograss Champion, from MelkshamDivide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP1 + 1 + 1 + 1 = 12?Assigning values to variables within other statements - RubyRuby off the Rails?Making best use of the new enthusiasm for MelkshamData that we use during our training courses, and other training resources