## Re: [Sbcl-help] Decimal point and decimal comma

 Re: [Sbcl-help] Decimal point and decimal comma From: Raymond Wiker - 2010-11-11 09:28:40 ```>From Henry Baker's "Pragmatic Parsing" paper: http://home.pipeline.com/~hbaker1/Prag-Parse.html --- it should be relatively easy to modify this to ignore a (configurable/parameterized) digit separator, and to have a (configurable/parameterized) decimal point character. BTW: Read this paper - it will do you good :-). In fact, read *all* Baker's papers. ============================= (deftype sign () '(member #\+ #\-)) (deftype expmarker () '(member #\e #\s #\f #\d #\l #\E #\S #\F #\D #\L)) (defun parse-number (&aux x (is #\+) id (i 0) dd (d 0) fd (f 0) (nf 0) (es #\+) ed (e 0) (m #\e)) ;;; Parse CL real number according to [Steele90,22.1.2] ;;; Return 2 values: the number and a reversed list of lookahead characters. (matchit [{[@(sign is) !(push is x)] []} ; scan sign. \$[@(digit id) !(setq x nil i (+ (* i 10) (ctoi id)))] ; integer digits. {[!id #\/ !(push #\/ x) ; "/" => ratio. \$[@(digit dd) !(setq x nil d (+ (* d 10) (ctoi dd)))]] ; denom. digits. [{[#\. {!id !(push #\. x)} ; decimal point. \$[@(digit fd) !(setq x nil nf (1+ nf) f (+ (* f 10) (ctoi fd)))]] ; fract. digits. []} {[{!id !fd} @(expmarker m) !(push m x) ; exp. marker. {[@(sign es) !(push es x)] []} ; exponent sign. \$[@(digit ed) !(setq x nil e (+ (* e 10) (ctoi ed)))]] ; exp. digits. []}]}]) (let ((sign (if (eql is #\-) -1 1)) (ex (if (eql es #\-) (- e) e))) (values (cond ((or fd ed) (make-float m sign i f nf ex)) ; see [Clinger90] (dd (/ (* sign i) d)) (id (* sign i)) (t nil)) x))) 2010/11/11 Slobodan Milnović : > On Thu, Nov 11, 2010 at 06:27, Christopher Stacy wrote: >> >> On Nov 10, 2010, at 10:57 PM, Pascal J. Bourguignon wrote: >> >>> Slobodan Milnović writes: >>> >>>> I have tried converting strings to numbers, and I have found the >>>> example using read-from-string. Unfortunately, it only accepts numbers >>>> formatted using decimal dot. >>> >>> What do you mean? >>> >>> >>> * (mapcar 'read-from-string '("123" "123.456" "1e6" "#36rHelloWorld" "42.")) >>> >>> (123 123.456 1000000.0 1767707668033969 42) >> >> I think he is asking for locale-sensitive numeric parsing, >> in particular (parse-number "1,000" :locale 'numeric) => 1000 > > Not exactly like that. Here in Croatia we are using decimal comma for > representing real numbers - in this particular case that you have > presented, for us the result would be 1 and not 1000. > > For example, let's take an monetary value, I think it would > demonstrate exactly what the problem is. In the USA, it would be > written something like this: > > \$1,000,000.00 > > but we would write the same value like this: > > \$1.000.000,00 > > And in this case the read-from-string that sbcl provides fails. Well, > it works for countries that use decimal dot for the representation of > the real numbers, but not for the rest of the world. :-) > > Well, it seems that if there is no locales support, it should be > written. Perhaps I'll tackle that problem when I get a bit more lisp > experience and free time. :-) > > For now, just printing out real numbers with decimal comma is good enough. :-) > > Thank you all for your examples, I will try them out! > > ------------------------------------------------------------------------------ > Centralized Desktop Delivery: Dell and VMware Reference Architecture > Simplifying enterprise desktop deployment and management using > Dell EqualLogic storage and VMware View: A highly scalable, end-to-end > client virtualization framework. Read more! > http://p.sf.net/sfu/dell-eql-dev2dev > _______________________________________________ > Sbcl-help mailing list > Sbcl-help@... > https://lists.sourceforge.net/lists/listinfo/sbcl-help > ```