set global default double -> string precision

172 weergaven
Naar het eerste ongelezen bericht

Rich Eakin

ongelezen,
12 jul 2015, 18:21:0312-07-2015
aan General Dart Discussion
I've been working on a simulation in dart and found the double.toStringAsFixed() method very useful in controlling how much data gets printout to console when I'm debugging variables. I was thinking though, it would be nice if it was possible to set a global precision for my application or isolate, so that I did not have to write that method in every print statement that I make. Is there any existing discussion for something like this, or a feature request?

cheers,
Rich  

Lasse R.H. Nielsen

ongelezen,
13 jul 2015, 05:14:5113-07-2015
aan mi...@dartlang.org
Not any request or discussion that I can remember.

If you have a fixed format that you tend to use, you could use a helper function to do the formatting for you:

int fixedPrecision = 3;
String toFixed(double value) => value.toStringAsFixed(fixedPrecision);

We don't have any printf, which is what you would normally use in other languages, because string interpolations does most of the job very well.
A case that it doesn't handle as well is number formatting, which gets pretty verbose.

/L

--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.



--
Lasse R.H. Nielsen - l...@google.com  
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K - Denmark - CVR nr. 28 86 69 84

kaop...@gmail.com

ongelezen,
13 jul 2015, 06:53:4113-07-2015
aan 'Lasse R.H. Nielsen' via Dart Misc
Adopt log library and extend it with your own implementation


寄件者: 'Lasse R.H. Nielsen' via Dart Misc
已傳送: 2015年7月13日星期一 17:14
收件者: mi...@dartlang.org
回覆至: mi...@dartlang.org
主旨: Re: [dart-misc] set global default double -> string precision

Ian Mackenzie

ongelezen,
13 jul 2015, 23:53:4713-07-2015
aan mi...@dartlang.org
If you're not using string interpolation and just calling print(someValue), then perhaps you could use a Zone to override the print() function within a body of code to recognize doubles and format them to a given precision? You could even get fancy with your overridden print() function to allow passing it lists of arguments, and it would concatenate the string versions of each while using your default precision for doubles, so you could call (within the zone)

print(['The value of x is ', x, ' and the value of y is ', y]);

to get a form of string interpolation (unless of course you also want to be able to print out lists as lists!).

Rich Eakin

ongelezen,
15 jul 2015, 19:13:5415-07-2015
aan General Dart Discussion
The zones is an interesting idea. Ideally though, I think it would be nice to use the built in print function or a custom function that took the same parameter (Object), and control how the formatting is done. The problem with using my own custom print function (or a logging library for that matter), is that as far as I can tell the double.toString()'s have already happened so my function only sees a big concatenated string.

--

Ian Mackenzie

ongelezen,
15 jul 2015, 23:43:3315-07-2015
aan mi...@dartlang.org
Well, I suppose if your custom print function gets passed a string, you could use a regular expression to find all substrings that look like printed double values, then parse those as doubles, convert them back to strings using your desired precision, then rebuild the argument string based on them before actually printing it...but of course that only works if you're looking to *decrease* precision from the default and would break if you ever tried to print something like "version 2.3".
Allen beantwoorden
Auteur beantwoorden
Doorsturen
0 nieuwe berichten