Number Format

217 views
Skip to first unread message

Werner

unread,
Sep 7, 2020, 12:09:26 PM9/7/20
to TiddlyWiki
Guys,

am I right that there is no TW-inherent way to display a number format or, better, locale-specific number format?

Wouldn't it be great if it was integral part of the <$view> widget, much like date format is?

Is there an easy fix? I mean, without having to install another plugin just for this purpose.

Thanks, Werner

TW Tones

unread,
Sep 7, 2020, 8:25:30 PM9/7/20
to TiddlyWiki
Werner

Perhaps some examples?

Evans formulae plugin has features to support this from memory. There are other maths plugins that may.

I can imagin a gap that exists between basic maths and the more advanced maths especialy in relation to display.

However i can see these gaps would be easy to solve with a few wiki text macros or taping into javascript functions that already handle it.

Please research your percived gaps and post back some details.

Tones

TW Tones

unread,
Sep 7, 2020, 8:32:41 PM9/7/20
to TiddlyWiki
Post script

Actualy i think using simple templates to display a number in a desired format would be smart and the template can be changed to suit localisation if not automaticaly respond to localisation. Macro access to templates is also easy to provide.

Tones

Werner

unread,
Sep 18, 2020, 9:20:14 AM9/18/20
to TiddlyWiki
Tony, 

sorry, was absorbed by other projects, so couldn't reply earlier. OK, I'll be a bit more specific:

I have a number stored in a JSON tiddler. When I retrieve it, I get plain old 3500 for example. But I want decimal points and comma separators to have it rendered like 3,500. Or 3.500 in German. Or whatever is your default locale. And, if DateFormat is included in the view widget, I think NumberFormat should also be. So I think, the most elegant solution would be extending the view widget source code to allow for "number" and pass it through to JavaScript intl.Numberformat (cf.: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat). Should not be that hard to implement. Maybe I try it, if I find the time.

Of course there are plugins, but I think something as basic as a number format should be in the core.

Werner

TW Tones

unread,
Sep 18, 2020, 9:58:18 AM9/18/20
to TiddlyWiki
Werner,

That's a reasonable argument. I understand the View Widget is considered "over loaded" so Perhaps a new widget for number formats would make sense.

However let me think if there are some snazzy work arounds for now.

Tones

Eric Shulman

unread,
Sep 19, 2020, 2:11:09 AM9/19/20
to TiddlyWiki
On Friday, September 18, 2020 at 6:20:14 AM UTC-7, Werner wrote:
I have a number stored in a JSON tiddler. When I retrieve it, I get plain old 3500 for example. But I want decimal points and comma separators to have it rendered like 3,500. Or 3.500 in German. Or whatever is your default locale. And, if DateFormat is included in the view widget, I think NumberFormat should also be. So I think, the most elegant solution would be extending the view widget source code to allow for "number" and pass it through to JavaScript intl.Numberformat (cf.: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat). Should not be that hard to implement. Maybe I try it, if I find the time.

Here's the basic code for a javascript macro, <<number>>.
/*\
title: Number.js
type: application/javascript
module-type: macro

returns a formatted number
\*/

(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports
.name   = "number";
exports
.params = [ { name: "value" }, { name: "country" }, { name: "style" }, { name: "currency" } ];
exports
.run    = function(value,country,style,currency) {
     
return Intl.NumberFormat( country||"en-US", { style:style||"decimal", currency:currency||"USD" } ).format(value);
   
};
})();

There's LOTS of "options" parameters that can be specified, but the above example only uses "style" and "currency".

Even so, this is sufficient to handle these uses:
<<number value:123456789.123456789 country:en-US style:currency currency:USD>>
<
<number value:123456789.123456789 country:de-DE style:currency currency:EUR>>
<
<number value:123456789.123456789 country:ja-JP style:currency currency:JPY>>

Here's a link to a reference for options that are available:

I leave the rest of the work of extending the macro to handle more options to whomever wishes to give it a go.

enjoy,
-e

TW Tones

unread,
Sep 19, 2020, 4:51:30 AM9/19/20
to TiddlyWiki
Werner,

As you say, would be good in the core, but almost as good is a simple macro.

See attached and import to any wiki, then open $:/PSaT/format-numbers For instructions

Syntax
<<format-number n [t] [d] [p]>>
  • n is the number with or without decimal places (no default)t is the thousands separator (optional) - defaults to ","
    • (Can change default in format-number macro, set thousands-separator)
  • d is the decimal separator (optional) - defaults to "."
    • (Can change default in format-number macro, set decimal-separator)
  • p is the decimal places to use (optional) - defaults to "2"
    • (Can change default in format-number macro, set decimal-places)
For example <<format-number 1234567654321.6666>> results in 1,234,567,654,321.67 
 
$:/PSaT/format-numbers/macro
\define format-number(n t d p)
<$set name=thousands-separator value="$t$" emptyValue=",">
<$set name=decimal-separator value="$d$" emptyValue=".">
<$set name=decimal-places value="$p$" emptyValue="2">
<$set name=fixed-decimal value={{{ [[$n$]fixed<decimal-places>] }}}>
<$set name=integer value={{{ [<fixed-decimal>split[.]first[]] }}}>
<$set name=remainder value={{{ [<fixed-decimal>split[.]last[]] }}}>
<$set name=length filter="[<integer>length[]]">
<$macrocall $name=each-digit length=<<length>> /><$list filter="[<decimal-places>!match[0]]" variable=nul><<decimal-separator>><<remainder>></$list>
</$set></$set></$set></$set></$set></$set></$set>
\end
\define each-digit(length)
\whitespace trim
<$list filter="[range[1,$length$]]" variable=position>
<$set name=digit filter="[[$(integer)$]split[]nth<position>]">
<$set name=zeros filter="[[$length$]subtract<position>]">
<<digit>>{{{ [<zeros>!match[0]remainder[3]match[0]then<thousands-separator>] }}}
</$set></$set>
</$list>
\end

Regards
Tones
On Friday, 18 September 2020 at 23:20:14 UTC+10 Werner wrote:

TW Tones

unread,
Sep 19, 2020, 4:53:45 AM9/19/20
to TiddlyWiki
Eric Beat me to it,

 should have refreshed GG, His solution is superior of course and worthy of the Core.

However mine still demonstrates the extendibility of tiddlywiki with only macros and wikitext.

Regards
Tones

Werner

unread,
Sep 21, 2020, 6:06:46 AM9/21/20
to TiddlyWiki
Thanks, guys. Great starting point.

Werner

The Islander

unread,
Nov 19, 2020, 1:19:56 AM11/19/20
to TiddlyWiki
Hi Tony,

Any idea why the comma separators are embedding a link in the comma? This is what seems to happen on my tiddlywiki.

Your Syntax writeup above also has a commas linking to a url for some odd reason as well.

Thx!
Reply all
Reply to author
Forward
0 new messages