A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
3
Task/Formatted-numeric-output/0DESCRIPTION
Normal file
3
Task/Formatted-numeric-output/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
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".
|
||||
4
Task/Formatted-numeric-output/1META.yaml
Normal file
4
Task/Formatted-numeric-output/1META.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- Text processing
|
||||
note: Basic language learning
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
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))
|
||||
)
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
BEGIN {
|
||||
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
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
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;
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
PROC newRealF(es, fl, digit, len=0, zeros=TRUE)
|
||||
DEF s, t, i
|
||||
IF (len = 0) OR (len < (digit+3))
|
||||
RETURN RealF(es, fl, digit)
|
||||
ELSE
|
||||
s := String(len)
|
||||
t := RealF(es, fl, digit)
|
||||
FOR i := 0 TO len-EstrLen(t)-1 DO StrAdd(s, IF zeros THEN '0' ELSE ' ')
|
||||
StrAdd(s, t)
|
||||
StrCopy(es, s)
|
||||
DisposeLink(s)
|
||||
DisposeLink(t)
|
||||
ENDIF
|
||||
ENDPROC es
|
||||
|
||||
PROC main()
|
||||
DEF s[100] : STRING
|
||||
WriteF('\s\n', newRealF(s, 7.125, 3,9))
|
||||
ENDPROC
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
'ZF15.9' ⎕FMT 7.125
|
||||
00007.125000000
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
MsgBox % pad(7.25,7) ; 0007.25
|
||||
MsgBox % pad(-7.25,7) ; -007.25
|
||||
|
||||
pad(x,len) { ; pad with 0's from left to len chars
|
||||
IfLess x,0, Return "-" pad(SubStr(x,2),len-1)
|
||||
VarSetCapacity(p,len,Asc("0"))
|
||||
Return SubStr(p x,1-len)
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
PRINT FNformat(PI, 9, 3)
|
||||
PRINT FNformat(-PI, 9, 3)
|
||||
END
|
||||
|
||||
DEF FNformat(n, sl%, dp%)
|
||||
LOCAL @%
|
||||
@% = &1020000 OR dp% << 8
|
||||
IF n >= 0 THEN
|
||||
= RIGHT$(STRING$(sl%,"0") + STR$(n), sl%)
|
||||
ENDIF
|
||||
= "-" + RIGHT$(STRING$(sl%,"0") + STR$(-n), sl%-1)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
#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;
|
||||
}
|
||||
11
Task/Formatted-numeric-output/C/formatted-numeric-output.c
Normal file
11
Task/Formatted-numeric-output/C/formatted-numeric-output.c
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#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;
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
(cl-format true "~9,3,,,'0F" 7.125)
|
||||
|
|
@ -0,0 +1 @@
|
|||
(printf "%09.3f" 7.125) ; format works the same way (without side the effect of printing)
|
||||
|
|
@ -0,0 +1 @@
|
|||
(format t "~9,3,,,'0F" 7.125)
|
||||
11
Task/Formatted-numeric-output/D/formatted-numeric-output.d
Normal file
11
Task/Formatted-numeric-output/D/formatted-numeric-output.d
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import std.stdio;
|
||||
|
||||
void main() {
|
||||
immutable r = 7.125;
|
||||
writefln(" %9.3f", -r);
|
||||
writefln(" %9.3f", r);
|
||||
writefln(" %-9.3f", r);
|
||||
writefln(" %09.3f", -r);
|
||||
writefln(" %09.3f", r);
|
||||
writefln(" %-09.3f", r);
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
program FormattedNumericOutput;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils;
|
||||
|
||||
const
|
||||
fVal = 7.125;
|
||||
|
||||
begin
|
||||
Writeln(FormatFloat('0000#.000',fVal));
|
||||
Writeln(FormatFloat('0000#.0000000',fVal));
|
||||
Writeln(FormatFloat('##.0000000',fVal));
|
||||
Writeln(FormatFloat('0',fVal));
|
||||
Writeln(FormatFloat('#.#E-0',fVal));
|
||||
Writeln(FormatFloat('#,##0.00;;Zero',fVal));
|
||||
Readln;
|
||||
end.
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
note
|
||||
description : "{
|
||||
2 Examples are given.
|
||||
The first example uses the standard library's FORMAT_DOUBLE class.
|
||||
The second example uses the AEL_PRINTF class from the freely available
|
||||
Amalasoft Eiffel Library (AEL).
|
||||
|
||||
See additional comments in the code.
|
||||
}"
|
||||
|
||||
class APPLICATION
|
||||
|
||||
inherit
|
||||
AEL_PRINTF -- Optional, see below
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
-- Run application.
|
||||
do
|
||||
print_formatted_std (7.125)
|
||||
print_formatted_ael (7.125)
|
||||
end
|
||||
|
||||
--|--------------------------------------------------------------
|
||||
|
||||
print_formatted_std (v: REAL_64)
|
||||
-- Print the value 'v' as a zero-padded string in a fixed
|
||||
-- overall width of 9 places and, with a precision of
|
||||
-- to 3 places to the right of the decimal point.
|
||||
-- Use the FORMAT_DOUBLE class from the standard library
|
||||
local
|
||||
fmt: FORMAT_DOUBLE
|
||||
do
|
||||
create fmt.make (9, 3)
|
||||
fmt.zero_fill
|
||||
print (fmt.formatted (v) + "%N")
|
||||
end
|
||||
|
||||
--|--------------------------------------------------------------
|
||||
|
||||
print_formatted_ael (v: REAL_64)
|
||||
-- Print the value 'v' as a zero-padded string in a fixed
|
||||
-- overall width of 9 places and, with a precision of
|
||||
-- to 3 places to the right of the decimal point.
|
||||
-- Use the AEL_PRINTF class from the Amalasoft Eiffel Library
|
||||
-- freely available from www.amalasoft.com
|
||||
do
|
||||
-- printf accepts a format string and an argument list
|
||||
-- The argument list is a container (often a manifest
|
||||
-- array) of values corresponding to the type of the format
|
||||
-- specified in the format string argument.
|
||||
-- When only one argument is needed, then there is also the
|
||||
-- option to use just the value, without the container.
|
||||
-- In this example, the line would be:
|
||||
-- printf ("%%09.3f%N", v)
|
||||
-- The more deliberate form is used in the actual example,
|
||||
-- as it is more representative of common usage, when there
|
||||
-- are multiple value arguments.
|
||||
|
||||
printf ("%%09.3f%N", << v >>)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
constant r = 7.125
|
||||
printf(1,"%9.3f\n",-r)
|
||||
printf(1,"%9.3f\n",r)
|
||||
printf(1,"%-9.3f\n",r)
|
||||
printf(1,"%09.3f\n",-r)
|
||||
printf(1,"%09.3f\n",r)
|
||||
printf(1,"%-09.3f\n",r)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
class Main
|
||||
{
|
||||
public static Void main()
|
||||
{
|
||||
echo (7.125.toStr.padl(9, '0'))
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
\ 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 ;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
Type: 123 s>d 8 ud.0
|
||||
Result: 00000123 ok
|
||||
Type: -123 s>d 8 d.0
|
||||
Result: -00000123 ok
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
INTEGER :: number = 7125
|
||||
WRITE(*,"(I8.8)") number ! Prints 00007125
|
||||
|
|
@ -0,0 +1 @@
|
|||
fmt.Printf("%09.3f", 7.125)
|
||||
|
|
@ -0,0 +1 @@
|
|||
printf ("%09.3f", 7.125)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
import Text.Printf
|
||||
main =
|
||||
printf "%09.3f" 7.125
|
||||
|
|
@ -0,0 +1 @@
|
|||
WRITE(ClipBoard, Format='i5.5, F4.3') INT(7.125), MOD(7.125, 1) ! 00007.125
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
n = 7.125
|
||||
print, n, format='(f08.3)'
|
||||
;==> 0007.125
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
link printf
|
||||
|
||||
procedure main()
|
||||
|
||||
every r := &pi | -r | 100-r do {
|
||||
write(r," <=== no printf")
|
||||
every p := "|%r|" | "|%9.3r|" | "|%-9.3r|" | "|%0.3r|" | "|%e|" | "|%d|" do
|
||||
write(sprintf(p,r)," <=== sprintf ",p)
|
||||
}
|
||||
end
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
'r<0>9.3' (8!:2) 7.125
|
||||
00007.125
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
import java.text.DecimalFormat;
|
||||
import java.text.NumberFormat;
|
||||
|
||||
public class Format {
|
||||
public static void main(String[] args){
|
||||
NumberFormat numForm = new DecimalFormat();
|
||||
numForm.setMinimumIntegerDigits(9);
|
||||
//Maximum also available for Integer digits and Fraction digits
|
||||
numForm.setGroupingUsed(false);//stops it from inserting commas
|
||||
System.out.println(numForm.format(7.125));
|
||||
|
||||
//example of Fraction digit options
|
||||
numForm.setMinimumIntegerDigits(5);
|
||||
numForm.setMinimumFractionDigits(5);
|
||||
System.out.println(numForm.format(7.125));
|
||||
numForm.setMinimumFractionDigits(0);
|
||||
numForm.setMaximumFractionDigits(2);
|
||||
System.out.println(numForm.format(7.125));
|
||||
System.out.println(numForm.format(7.135));//rounds to even
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
var n = 123;
|
||||
var str = ("00000" + n).slice(-5);
|
||||
alert(str);
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
for i =1 to 10
|
||||
n =rnd( 1) *10^( int( 10 *rnd(1)) -2)
|
||||
print "Raw number ="; n; "Using custom function ="; FormattedPrint$( n, 16, 5)
|
||||
next i
|
||||
end
|
||||
|
||||
function FormattedPrint$( n, length, decPlaces)
|
||||
format$ ="#."
|
||||
for i =1 to decPlaces
|
||||
format$ =format$ +"#"
|
||||
next i
|
||||
|
||||
n$ =using( format$, n) ' remove leading spaces if less than 3 figs left of decimal
|
||||
' add leading zeros
|
||||
for i =1 to len( n$)
|
||||
c$ =mid$( n$, i, 1)
|
||||
if c$ =" " or c$ ="%" then nn$ =nn$ +"0" else nn$ =nn$ +c$
|
||||
next i
|
||||
FormattedPrint$ =right$( "000000000000" +nn$, length) ' chop to required length
|
||||
end function
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
to zpad :num :width :precision
|
||||
output map [ifelse ? = "| | ["0] [?]] form :num :width :precision
|
||||
end
|
||||
print zpad 7.125 9 3 ; 00007.125
|
||||
|
|
@ -0,0 +1 @@
|
|||
print form 7.125 -1 "|%09.3f| ; 00007.125
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
function digits(n) return math.floor(math.log(n) / math.log(10))+1 end
|
||||
function fixedprint(num, digs) --digs = number of digits before decimal point
|
||||
for i = 1, digs - digits(num) do
|
||||
io.write"0"
|
||||
end
|
||||
print(num)
|
||||
end
|
||||
|
||||
fixedprint(7.125, 5) --> 00007.125
|
||||
|
|
@ -0,0 +1 @@
|
|||
print(string.format("%09.3d",7.125))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
>> disp(sprintf('%09.3f',7.125))
|
||||
00007.125
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
StringJoin[ToString /@ PadLeft[Characters[ToString[ 7.125]], 9]]
|
||||
->"00007.125"
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
:- module formatted_numeric_output.
|
||||
:- interface.
|
||||
:- import_module io.
|
||||
|
||||
:- pred main(io::di, io::uo) is det.
|
||||
|
||||
:- implementation.
|
||||
:- import_module list, string.
|
||||
|
||||
main(!IO) :-
|
||||
io.format("%09.3f\n", [f(7.125)], !IO).
|
||||
|
|
@ -0,0 +1 @@
|
|||
IO.Put(Fmt.Pad("7.125\n", length := 10, padChar := '0'));
|
||||
|
|
@ -0,0 +1 @@
|
|||
echo str_pad(7.125, 9, '0', STR_PAD_LEFT);
|
||||
|
|
@ -0,0 +1 @@
|
|||
printf("%09.3f\n", 7.125);
|
||||
|
|
@ -0,0 +1 @@
|
|||
printf "%09.3f\n", 7.125;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(pad 9 (format 7125 3))
|
||||
(pad 9 (format 7125 3 ",")) # European format
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
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)
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
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))
|
||||
28
Task/Formatted-numeric-output/R/formatted-numeric-output-1.r
Normal file
28
Task/Formatted-numeric-output/R/formatted-numeric-output-1.r
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
> sprintf("%f", pi)
|
||||
[1] "3.141593"
|
||||
> sprintf("%.3f", pi)
|
||||
[1] "3.142"
|
||||
> sprintf("%1.0f", pi)
|
||||
[1] "3"
|
||||
> sprintf("%5.1f", pi)
|
||||
[1] " 3.1"
|
||||
> sprintf("%05.1f", pi)
|
||||
[1] "003.1"
|
||||
> sprintf("%+f", pi)
|
||||
[1] "+3.141593"
|
||||
> sprintf("% f", pi)
|
||||
[1] " 3.141593"
|
||||
> sprintf("%-10f", pi)# left justified
|
||||
[1] "3.141593 "
|
||||
> sprintf("%e", pi)
|
||||
[1] "3.141593e+00"
|
||||
> sprintf("%E", pi)
|
||||
[1] "3.141593E+00"
|
||||
> sprintf("%g", pi)
|
||||
[1] "3.14159"
|
||||
> sprintf("%g", 1e6 * pi) # -> exponential
|
||||
[1] "3.14159e+06"
|
||||
> sprintf("%.9g", 1e6 * pi) # -> "fixed"
|
||||
[1] "3141592.65"
|
||||
> sprintf("%G", 1e-6 * pi)
|
||||
[1] "3.14159E-06"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
formatC(x, width=9, flag="0")
|
||||
# "00007.125"
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
/*REXX program shows various ways to add leading zeroes to numbers. */
|
||||
a=7.125
|
||||
b=translate(format(a,10),0,' ')
|
||||
say 'a=' a
|
||||
say 'b=' b
|
||||
say
|
||||
|
||||
c=8.37
|
||||
d=right(c,20,0)
|
||||
say 'c=' c
|
||||
say 'd=' d
|
||||
say
|
||||
|
||||
e=19.46
|
||||
f='000000'e
|
||||
say 'e=' e
|
||||
say 'f=' f
|
||||
say
|
||||
|
||||
g=18.25e+1
|
||||
h=000000||g
|
||||
say 'g=' g
|
||||
say 'h=' h
|
||||
say
|
||||
|
||||
i=45.2
|
||||
j=translate(' 'i,0," ")
|
||||
say 'i=' i
|
||||
say 'j=' j
|
||||
say
|
||||
|
||||
k=36.007
|
||||
l=insert(00000000,k,0)
|
||||
say 'k=' k
|
||||
say 'l=' l
|
||||
say
|
||||
|
||||
m=.10055
|
||||
n=copies(0,20)m
|
||||
say 'm=' m
|
||||
say 'n=' n
|
||||
say
|
||||
|
||||
p=4.060
|
||||
q=0000000000000||p
|
||||
say 'p=' p
|
||||
say 'q=' q
|
||||
say
|
||||
|
||||
r=876
|
||||
s=substr(r+10000000,2)
|
||||
say 'r=' r
|
||||
say 's=' s
|
||||
say
|
||||
|
||||
t=13.02
|
||||
u=reverse(reverse(t)000000000)
|
||||
say 't=' t
|
||||
say 'u=' u
|
||||
/*stick a fork in it, we're done.*/
|
||||
|
|
@ -0,0 +1 @@
|
|||
printf " %09.3f\n", 7.125
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
class MAIN is
|
||||
main is
|
||||
#OUT + #FMT("<F0 #####.###>", 7.1257) + "\n";
|
||||
#OUT + #FMT("<F0 #####.###>", 7.1254) + "\n";
|
||||
end;
|
||||
end;
|
||||
|
|
@ -0,0 +1 @@
|
|||
#OUT + #FMT("%09.3f", 7.125) + "\n";
|
||||
|
|
@ -0,0 +1 @@
|
|||
println("%09.3f".format(7.125)) //-> 00007.125
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
Transcript show: (7.125 printPaddedWith: $0 to: 3.6); cr.
|
||||
"output: 007.125000"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(7.123 asFixedPoint:3) printOn: Transcript leftPaddedTo: 9 with: $0
|
||||
"output: 00007.125"
|
||||
|
|
@ -0,0 +1 @@
|
|||
PrintfScanf new printf:'%08.3f' arguments: { 7.125 }
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
set number 7.342
|
||||
format "%08.3f" $number
|
||||
Loading…
Add table
Add a link
Reference in a new issue