Formatted numeric output: Difference between revisions
(added common lisp) |
No edit summary |
||
Line 5: | Line 5: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<ada>with Ada.Text_Io.Editing; use Ada.Text_Io.Editing; |
<lang ada>with Ada.Text_Io.Editing; use Ada.Text_Io.Editing; |
||
with Ada.Text_Io; use Ada.Text_Io; |
with Ada.Text_Io; use Ada.Text_Io; |
||
Line 18: | Line 18: | ||
begin |
begin |
||
Put(Item => Value, Pic => Pic); |
Put(Item => Value, Pic => Pic); |
||
end Zero_Fill;</ |
end Zero_Fill;</lang> |
||
The output of this program is |
The output of this program is |
||
Line 69: | Line 69: | ||
=={{header|C}}== |
=={{header|C}}== |
||
<c>#include <stdio.h> |
<lang c>#include <stdio.h> |
||
main(){ |
main(){ |
||
float r=7.125; |
float r=7.125; |
||
Line 79: | Line 79: | ||
printf(" %-09.3f\n",r); |
printf(" %-09.3f\n",r); |
||
return 0; |
return 0; |
||
}</ |
}</lang> |
||
Output: |
Output: |
||
-7.125 |
-7.125 |
||
Line 89: | Line 89: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
<cpp>#include <iostream> |
<lang cpp>#include <iostream> |
||
#include <iomanip> |
#include <iomanip> |
||
Line 96: | Line 96: | ||
std::cout << std::setfill('0') << std::setw(9) << std::fixed << std::setprecision(3) << 7.125 << std::endl; |
std::cout << std::setfill('0') << std::setw(9) << std::fixed << std::setprecision(3) << 7.125 << std::endl; |
||
return 0; |
return 0; |
||
}</ |
}</lang> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
<lisp>(format t "~9,3,,,'0F" 7.125)</ |
<lang lisp>(format t "~9,3,,,'0F" 7.125)</lang> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
Line 147: | Line 147: | ||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
Stealing printf from C/C++: |
Stealing printf from C/C++: |
||
<java>public class Printing{ |
<lang java>public class Printing{ |
||
public static void main(String[] args){ |
public static void main(String[] args){ |
||
double printer = 7.125; |
double printer = 7.125; |
||
System.out.printf("%09.3f",printer);//System.out.format works the same way |
System.out.printf("%09.3f",printer);//System.out.format works the same way |
||
} |
} |
||
}</ |
}</lang> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
<javascript>var n = 123; |
<lang javascript>var n = 123; |
||
var str = ("00000" + n).slice(-5); |
var str = ("00000" + n).slice(-5); |
||
alert(str);</ |
alert(str);</lang> |
||
or, put in browser URL: javascript:n=123;alert(("00000"+n).slice(-5)); |
or, put in browser URL: javascript:n=123;alert(("00000"+n).slice(-5)); |
||
Line 180: | Line 180: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<ocaml>Printf.printf "%09.3f\n" 7.125</ |
<lang ocaml>Printf.printf "%09.3f\n" 7.125</lang> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{works with|Perl|5.x}} |
{{works with|Perl|5.x}} |
||
<perl>printf " %09.3f\n", 7.125;</ |
<lang perl>printf " %09.3f\n", 7.125;</lang> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
<php>echo str_pad(7.125, 9, '0', STR_PAD_LEFT);</ |
<lang php>echo str_pad(7.125, 9, '0', STR_PAD_LEFT);</lang> |
||
or |
or |
||
<php>printf("%09.3f\n", 7.125);</ |
<lang php>printf("%09.3f\n", 7.125);</lang> |
||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
Line 220: | Line 220: | ||
specifying how many digits appear in the exponent when printed with a format. |
specifying how many digits appear in the exponent when printed with a format. |
||
<python>from math import pi, exp |
<lang python>from math import pi, exp |
||
r = exp(pi)-pi |
r = exp(pi)-pi |
||
print r |
print r |
||
Line 229: | Line 229: | ||
print "e=%09.4e f=%09.4f g=%09.4g!"%(-r,-r,-r) |
print "e=%09.4e f=%09.4f g=%09.4g!"%(-r,-r,-r) |
||
print "e=%09.4e f=%09.4f g=%09.4g!"%(r,r,r) |
print "e=%09.4e f=%09.4f g=%09.4g!"%(r,r,r) |
||
print "e=%-09.4e f=%-09.4f g=%-09.4g!"%(r,r,r)</ |
print "e=%-09.4e f=%-09.4f g=%-09.4g!"%(r,r,r)</lang> |
||
<pre> |
<pre> |
||
19.9990999792 |
19.9990999792 |
||
Line 242: | Line 242: | ||
{{works with|Python|3}} |
{{works with|Python|3}} |
||
<python>from math import pi, exp |
<lang python>from math import pi, exp |
||
r = exp(pi)-pi |
r = exp(pi)-pi |
||
print(r) |
print(r) |
||
Line 251: | Line 251: | ||
print("e={0:09.4e} f={0:09.4f} g={0:09.4g}!".format(-r)) |
print("e={0:09.4e} f={0:09.4f} g={0:09.4g}!".format(-r)) |
||
print("e={0:09.4e} f={0:09.4f} g={0:09.4g}!".format(r)) |
print("e={0:09.4e} f={0:09.4f} g={0:09.4g}!".format(r)) |
||
print("e={0:-09.4e} f={0:-09.4f} g={0:-09.4g}!".format(r))</ |
print("e={0:-09.4e} f={0:-09.4f} g={0:-09.4g}!".format(r))</lang> |
||
<pre> |
<pre> |
||
19.9990999792 |
19.9990999792 |
||
Line 269: | Line 269: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<ruby>printf " %09.3f\n", 7.125</ |
<lang ruby>printf " %09.3f\n", 7.125</lang> |
||
=={{header|SQL}}== |
=={{header|SQL}}== |
Revision as of 15:33, 3 February 2009
You are encouraged to solve this task according to the task description, using any language you may know.
Express a number in decimal as a fixed-length string with leading zeros.
For example, the number 7.125 could be expressed as "00007.125".
Ada
<lang ada>with Ada.Text_Io.Editing; use Ada.Text_Io.Editing; with Ada.Text_Io; use Ada.Text_Io;
procedure Zero_Fill is
Pic_String: String := "<999999.99>"; Pic : Picture := To_Picture(Pic_String); type Money is delta 0.01 digits 8; package Money_Output is new Decimal_Output(Money); use Money_Output; Value : Money := 37.25;
begin
Put(Item => Value, Pic => Pic);
end Zero_Fill;</lang> The output of this program is
000037.25
ALGOL 68
main:( REAL r=exp(pi)-pi; print((r,newline)); printf(($g(-16,4)l$,-r)); printf(($g(-16,4)l$,r)); printf(($g( 16,4)l$,r)); printf(($g( 16,4,1)l$,r)); printf(($-dddd.ddddl$,-r)); printf(($-dddd.ddddl$,r)); printf(($+dddd.ddddl$,r)); printf(($ddddd.ddddl$,r)); printf(($zzzzd.ddddl$,r)); printf(($zzzz-d.ddddl$,r)); printf(($zzzz-d.ddddedl$,r)); printf(($zzzz-d.ddddeddl$,r)); printf(($4z-d.4de4dl$,r)) )
Output:
+1.99990999791895e +1 -19.9991 19.9991 +19.9991 +19999099.979e-6 -0019.9991 0019.9991 +0019.9991 00019.9991 00019.9991 19.9991 1.9999e1 1.9999e01 1.9999e0001
APL
'ZF15.9' ⎕FMT 7.125 00007.125000000
APL's ⎕FMT is similar to C's printf (only it operates on arrays).
C
<lang c>#include <stdio.h> main(){
float r=7.125; printf(" %9.3f\n",-r); printf(" %9.3f\n",r); printf(" %-9.3f\n",r); printf(" %09.3f\n",-r); printf(" %09.3f\n",r); printf(" %-09.3f\n",r); return 0;
}</lang> Output:
-7.125 7.125 7.125 -0007.125 00007.125 7.125
C++
<lang cpp>#include <iostream>
- include <iomanip>
int main() {
std::cout << std::setfill('0') << std::setw(9) << std::fixed << std::setprecision(3) << 7.125 << std::endl; return 0;
}</lang>
Common Lisp
<lang lisp>(format t "~9,3,,,'0F" 7.125)</lang>
Forth
Forth has a rather rich set of number formatting words, which makes formatted output very flexible but sometime cumbersome.
Here one way to generate the required output. Note that the number generated is NOT truncated to the field width. If you wish to truncate the number, remove #s and 1- from the definition. (The 1- is necessary because #s always generates at least one digit, even if it's zero.)
\ format 'n' digits of the double word 'd' : #n ( d n -- d ) 0 ?do # loop ; \ ud.0 prints an unsigned double : ud.0 ( d n -- ) <# 1- #n #s #> type ; \ d.0 prints a signed double : d.0 ( d n -- ) >r tuck dabs <# r> 1- #n #s rot sign #> type ;
Usage example:
Type: 123 s>d 8 ud.0 Result: 00000123 ok Type: -123 s>d 8 d.0 Result: -00000123 ok
Fortran
Using standard data edit descriptors it is only possible to precede Integer data with leading zeros.
INTEGER :: number = 7125 WRITE(*,"(I8.8)") number ! Prints 00007125
Haskell
import Text.Printf main = printf "%09.3f" 7.125
IDL
n = 7.125 print, n, format='(f08.3)' ;==> 0007.125
J
'r<0>15.9' (8!:2) 7.125 00007.125000000
Java
Stealing printf from C/C++: <lang java>public class Printing{ public static void main(String[] args){ double printer = 7.125; System.out.printf("%09.3f",printer);//System.out.format works the same way } }</lang>
JavaScript
<lang javascript>var n = 123; var str = ("00000" + n).slice(-5); alert(str);</lang>
or, put in browser URL: javascript:n=123;alert(("00000"+n).slice(-5));
Logo
Various collection functions, such as MAP and FILTER, will work on individual characters of a string when given a word instead of a list.
to zpad :num :width :precision output map [ifelse ? = "| | ["0] [?]] form :num :width :precision end print zpad 7.125 9 3 ; 00007.125
As a debugging feature, you can drop down to C language printf formatting by giving -1 for the width and a format string for the precision.
print form 7.125 -1 "|%09.3f| ; 00007.125
Oberon-2
Module Out must be imported before use.
Out.Real(7.125, 9, 0);
OCaml
<lang ocaml>Printf.printf "%09.3f\n" 7.125</lang>
Perl
<lang perl>printf " %09.3f\n", 7.125;</lang>
PHP
<lang php>echo str_pad(7.125, 9, '0', STR_PAD_LEFT);</lang> or <lang php>printf("%09.3f\n", 7.125);</lang>
Pop11
The task is underspecified, so we present a few alternatives.
;;; field of length 12, 3 digits after decimal place format_print('~12,3,0,`*,`0F', [1299.19]); ;;; prints "00001299.190" format_print('~12,3,0,`*,`0F', [100000000000000000]); ;;; Since the number does not fit into the field prints "************" ;;; that is stars instead of the number format_print('~12,3,0,`*,`0F', [-1299.19]); ;;; prints "000-1299.190" ;;; that is _leading zeros_ before sign
format_print('~3,1,12,`0:$', [1299.19]); ;;; prints "00001299.190" format_print('~3,1,12,`0:$', [-1299.19]); ;;; prints "-0001299.190" ;;; that is sign before leading zeros format_print('~3,1,12,`0:$', [100000000000000000]); ;;; prints "100000000000000000.000" ;;; that is uses more space if the number does not fit into ;;; fixed width
Python
Python has 3 different floating point formatting methods: "%e","%f" & "%g". The "%g" format is a beautified hybrid of "%e" and "%f". There is no way of specifying how many digits appear in the exponent when printed with a format.
<lang python>from math import pi, exp r = exp(pi)-pi print r print "e=%e f=%f g=%g G=%G s=%s r=%r!"%(r,r,r,r,r,r) print "e=%9.4e f=%9.4f g=%9.4g!"%(-r,-r,-r) print "e=%9.4e f=%9.4f g=%9.4g!"%(r,r,r) print "e=%-9.4e f=%-9.4f g=%-9.4g!"%(r,r,r) print "e=%09.4e f=%09.4f g=%09.4g!"%(-r,-r,-r) print "e=%09.4e f=%09.4f g=%09.4g!"%(r,r,r) print "e=%-09.4e f=%-09.4f g=%-09.4g!"%(r,r,r)</lang>
19.9990999792 e=1.999910e+01 f=19.999100 g=19.9991 G=19.9991 s=19.9990999792 r=19.999099979189474! e=-1.9999e+01 f= -19.9991 g= -20! e=1.9999e+01 f= 19.9991 g= 20! e=1.9999e+01 f=19.9991 g=20 ! e=-1.9999e+01 f=-019.9991 g=-00000020! e=1.9999e+01 f=0019.9991 g=000000020! e=1.9999e+01 f=19.9991 g=20 !
<lang python>from math import pi, exp r = exp(pi)-pi print(r) print("e={0:e} f={0:f} g={0:g} G={0:G} s={0!s} r={0!r}!".format(r)) print("e={0:9.4e} f={0:9.4f} g={0:9.4g}!".format(-r)) print("e={0:9.4e} f={0:9.4f} g={0:9.4g}!".format(r)) print("e={0:-9.4e} f={0:-9.4f} g={0:-9.4g}!".format(r)) print("e={0:09.4e} f={0:09.4f} g={0:09.4g}!".format(-r)) print("e={0:09.4e} f={0:09.4f} g={0:09.4g}!".format(r)) print("e={0:-09.4e} f={0:-09.4f} g={0:-09.4g}!".format(r))</lang>
19.9990999792 e=1.999910e+01 f=19.999100 g=19.9991 G=19.9991 s=19.9990999792 r=19.999099979189474! e=-1.9999e+01 f= -19.9991 g= -20! e=1.9999e+01 f= 19.9991 g= 20! e=1.9999e+01 f=19.9991 g=20 ! e=-1.9999e+01 f=-019.9991 g=-00000020! e=1.9999e+01 f=0019.9991 g=000000020! e=1.9999e+01 f=19.9991 g=20 !
Raven
7.125 "%09.3f" print
00007.125
Ruby
<lang ruby>printf " %09.3f\n", 7.125</lang>
SQL
declare @n int select @n=123 select substring(convert(char(5), 10000+@n),2,4) as FourDigits set @n=5 print "TwoDigits: " + substring(convert(char(3), 100+@n),2,2) --Output: 05
Tcl
set number 7.342 format "%08.3f" $number
Toka
needs values value n 123 to n 2 import printf " %08d" n printf
XSLT
<xsl:value-of select="format-number(7.125, '00000000.#############')" />