comma as decimal separator

A recent bug fix in the Audacity development code (2.0.5 alpha) enables comma as the decimal separator when appropriate to the locale (for example German localization). However, for Nyquist plug-ins this only affects slider widgets. If numerical data is entered in a text widget, then Nyquist reads it as text and it is up to the plug-in author to handle conversion from string to number.

As with other programming languages, Nyquist only accepts a dot as the decimal separator, thus if a comma is to be allowed in the user interface, it must be converted to a dot before use. For slider widgets this is now handled automatically by Audacity (should be available in 2.0.5). For text widgets, it must be handled in the Nyquist code.

The following two functions will convert commas into dots, but only if there is one and only one comma in the text. This may be useful for handling commas as decimal separator.

The two functions are very similar. The first is shorter and the second is slightly more efficient (though the practical difference is likely to be negligible).

;;; Replace comma with dot if only one comma
(defun comma2dot (txt)
  (let ((c (string-search "," txt)))
    (if (and c (not (string-search "," (subseq txt (1+ c)))))
        (strcat (subseq txt 0 c) "." (subseq txt (1+ c)))
        txt)))



;;; Replace comma with dot if only one comma
(defun comma2dot (txt)
  (let ((c (string-search "," txt)))
    (if c
        (let ((rest (subseq txt (1+ c))))
          (if (string-search "," rest)
              txt
              (strcat (subseq txt 0 c) "." rest)))
        txt)))

To use thes functions, simply pass the string to be converted, for example:

(setq number-string "123,4")

;;; Replace comma with dot if only one comma
(defun comma2dot (txt)
  (let ((c (string-search "," txt)))
    (if (and c (not (string-search "," (subseq txt (1+ c)))))
        (strcat (subseq txt 0 c) "." (subseq txt (1+ c)))
        txt)))

(print (comma2dot number-string))

This topic links to bug 661 on bugzilla: http://bugzilla.audacityteam.org/show_bug.cgi?id=661