set global default double -> string precision

瀏覽次數:172 次
跳到第一則未讀訊息

Rich Eakin

未讀,
2015年7月12日 下午6:21:032015/7/12
收件者: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

未讀,
2015年7月13日 清晨5:14:512015/7/13
收件者: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

未讀,
2015年7月13日 清晨6:53:412015/7/13
收件者:'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

未讀,
2015年7月13日 晚上11:53:472015/7/13
收件者: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

未讀,
2015年7月15日 晚上7:13:542015/7/15
收件者: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

未讀,
2015年7月15日 晚上11:43:332015/7/15
收件者: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".
回覆所有人
回覆作者
轉寄
0 則新訊息