Formatted numeric output: Difference between revisions
Content added Content deleted
No edit summary |
m (Switch to header template) |
||
Line 4: | Line 4: | ||
For example, the number 7.125 could be expressed as "00007.125". |
For example, the number 7.125 could be expressed as "00007.125". |
||
== |
=={{header|Ada}}== |
||
[[Category:Ada]] |
|||
with Ada.Text_Io.Editing; use Ada.Text_Io.Editing; |
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 25: | Line 23: | ||
000037.25 |
000037.25 |
||
== |
=={{header|C}}== |
||
[[Category:C]] |
|||
void printit(int val) |
void printit(int val) |
||
{ |
{ |
||
Line 33: | Line 29: | ||
} |
} |
||
== |
=={{header|Forth}}== |
||
[[Category:Forth]] |
|||
Forth has a rather rich set of number formatting words, which makes formatted output very flexible but sometime cumbersome. |
Forth has a rather rich set of number formatting words, which makes formatted output very flexible but sometime cumbersome. |
||
Line 56: | Line 50: | ||
Result: -00000123 ok |
Result: -00000123 ok |
||
== |
=={{header|IDL}}== |
||
[[Category:IDL]] |
[[Category:IDL]] |
||
Line 63: | Line 57: | ||
;==> 0007.125 |
;==> 0007.125 |
||
== |
=={{header|JavaScript}}== |
||
[[Category:JavaScript]] |
|||
var n = 123; |
var n = 123; |
||
var str = ("00000" + n).slice(-5); |
var str = ("00000" + n).slice(-5); |
||
Line 72: | Line 64: | ||
//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)); |
||
== |
=={{header|Perl}}== |
||
[[Category:Perl]] |
|||
'''Interpreter:''' [[Perl]] v5.x |
'''Interpreter:''' [[Perl]] v5.x |
||
Line 80: | Line 71: | ||
# $pad is now 00123.45 |
# $pad is now 00123.45 |
||
== |
=={{header|Pop11}}== |
||
[[Category:Pop11]] |
|||
The task is underspecified, so we present a few alternatives. |
The task is underspecified, so we present a few alternatives. |
||
Line 105: | Line 94: | ||
;;; fixed width |
;;; fixed width |
||
== |
=={{header|Python}}== |
||
[[Category:Python]] |
|||
#tested in Python 2.4.1 |
#tested in Python 2.4.1 |
||
n = 123 |
n = 123 |
||
Line 113: | Line 101: | ||
#output: 123 00123 |
#output: 123 00123 |
||
== |
=={{header|Raven}}== |
||
[[Category:Raven]] |
|||
7.125 "%09.3f" print |
7.125 "%09.3f" print |
||
00007.125 |
00007.125 |
||
== |
=={{header|SQL}}== |
||
==={{header|MS SQL}} 2005=== |
|||
[[Category:SQL]] |
|||
--Tested MS SQL 2005 |
|||
declare @n int |
declare @n int |
||
select @n=123 |
select @n=123 |
||
Line 132: | Line 116: | ||
--Output: 05 |
--Output: 05 |
||
== |
=={{header|Tcl}}== |
||
[[Category:Tcl]] |
|||
set number 7.342 |
set number 7.342 |
||
format "%08.3f" $number |
format "%08.3f" $number |
||
== |
=={{header|Toka}}== |
||
[[Category:Toka]] |
|||
needs values |
needs values |
||
value n |
value n |
Revision as of 05:35, 13 November 2007
Formatted numeric output
You are encouraged to solve this task according to the task description, using any language you may know.
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
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;
The output of this program is
000037.25
C
void printit(int val) { printf ("%08d", val); // Prints 8 digits with leading zeros }
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
IDL
n = 7.125 print, n, format='(f08.3)' ;==> 0007.125
JavaScript
var n = 123; var str = ("00000" + n).slice(-5); alert(str);
//or, put in browser URL: javascript:n=123;alert(("00000"+n).slice(-5));
Perl
Interpreter: Perl v5.x
my $num = 123.45 my $pad = '0' x (5 - length(int($num))) . $num; # $pad is now 00123.45
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
#tested in Python 2.4.1 n = 123 str = "%05d" % n #5 places wide, leading zeros print n, str #output: 123 00123
Raven
7.125 "%09.3f" print
00007.125
SQL
MS SQL 2005
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