1:- module(wl_numbers,
    2	  [ n//2,			% +Format, +Value
    3	    nc//2,			% +Format, +Value
    4	    nc//3			% +Format, +Value, +Options
    5	  ]).

Tools for formatting numbers

Part of Weblog

Derived from

Part of ClioPatria SeRQL and SPARQL server

Author: Jan Wielemaker E-mail: J.Wielemaker@cs.vu.nl WWW: http://www.swi-prolog.org Copyright (C): 2010 VU University Amsterdam

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

As a special exception, if you link this library with other files, compiled with a Free Software compiler, to produce an executable, this library does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License. */

   42:- use_module(library(http/html_write)).   43:- use_module(library(sgml)).   44:- use_module(library(lists)).   45:- use_module(library(option)).   46:- use_module(library(occurs)).   47:- use_module(library(http/http_dispatch)).   48:- use_module(library(http/http_wrapper)).   49
   50:- html_meta((
   51	form_input(html, html, ?, ?),
   52	odd_even_row(+, -, html, ?, ?),
   53	sort_th(+, +, html, ?, ?))).
 nc(+Format, +Value)// is det
 nc(+Format, +Value, +Options)// is det
Numeric cell. The value is formatted using Format and right-aligned in a table cell (td).
Arguments:
Format- is a (numeric) format as described by format/2 or the constant human. Human formatting applies to integers and prints then in abreviated (K,M,T) form, e.g., 4.5M for 4.5 million.
Options- is passed as attributed to the td element. Default alignment is right.
   68nc(Fmt, Value) -->
   69	nc(Fmt, Value, []).
   70
   71nc(Fmt, Value, Options) -->
   72	{ class(Value, Class),
   73	  merge_options(Options,
   74			[ align(right),
   75			  class(Class)
   76			], Opts),
   77	  number_html(Fmt, Value, HTML)
   78	},
   79	html(td(Opts, HTML)).
   80
   81class(Value, Class) :-
   82	(   integer(Value)
   83	->  Class = int
   84	;   float(Value)
   85	->  Class = float
   86	;   Class = value
   87	).
 n(+Format, +Value)//
HTML component to emit a number.
See also
- nc//2 for details.
   95n(Fmt, Value) -->
   96	{ number_html(Fmt, Value, HTML) },
   97	html(HTML).
   98
   99number_html(human, Value, HTML) :-
  100	integer(Value), !,
  101	human_count(Value, HTML).
  102number_html(Fmt, Value, HTML) :-
  103	number(Value), !,
  104	HTML = Fmt-[Value].
  105number_html(_, Value, '~p'-[Value]).
  106
  107
  108human_count(Number, HTML) :-
  109	Number < 1024, !,
  110	HTML = '~d'-[Number].
  111human_count(Number, HTML) :-
  112	Number < 1024*1024, !,
  113	KB is Number/1024,
  114	digits(KB, N),
  115	HTML = '~*fK'-[N, KB].
  116human_count(Number, HTML) :-
  117	Number < 1024*1024*1024, !,
  118	MB is Number/(1024*1024),
  119	digits(MB, N),
  120	HTML = '~*fM'-[N, MB].
  121human_count(Number, HTML) :-
  122	TB is Number/(1024*1024*1024),
  123	digits(TB, N),
  124	HTML = '~*fG'-[N, TB].
  125
  126digits(Count, N) :-
  127	(   Count < 100
  128	->  N = 1
  129	;   N = 0
  130	)