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:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat
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
--
You received this message because you are subscribed to the Google Groups
"TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/tiddlywiki/828c9130-712a-48fd-b6e4-f1f6de3718a1o%40googlegroups.com.