GNU bug report logs - #59531
29.0.50: An alternative to `string-to-number` which throws an error (or returns a NIL value) when input is non-parseable as number

Previous Next

Package: emacs;

Reported by: Ramesh Nedunchezian <rameshnedunchezian <at> outlook.com>

Date: Thu, 24 Nov 2022 06:21:02 UTC

Severity: wishlist

Found in version 29.0.50

To reply to this bug, email your comments to 59531 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#59531; Package emacs. (Thu, 24 Nov 2022 06:21:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Ramesh Nedunchezian <rameshnedunchezian <at> outlook.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Thu, 24 Nov 2022 06:21:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Ramesh Nedunchezian <rameshnedunchezian <at> outlook.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 29.0.50: An alternative to `string-to-number` which throws an error
 (or returns a NIL value) when input is non-parseable as number
Date: Thu, 24 Nov 2022 11:44:43 +0530
`string-to-number` returns ZERO if the input is not a number.  


This return value is not very helpful.  The choice of a number ZERO as "Not A Number" doesn't help one to distinguish between the following two cases

(1) Input was a valid number, and it parses to number zero

(2) Input was NOT a valid number, and it was forcibly reported as ZERO

Consider amending `string-to-number` to throw an error (or return NIL) when the input is not parseable as a number, or providing an alternative API to validate numbers.  I am trying to parse some fields in an org table, and see if the field value is a number or not;

If there is already an alternative to what I am trying to accomplish, I would appreciate a recipe.







Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#59531; Package emacs. (Thu, 24 Nov 2022 08:00:02 GMT) Full text and rfc822 format available.

Message #8 received at 59531 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Ramesh Nedunchezian <rameshnedunchezian <at> outlook.com>
Cc: 59531 <at> debbugs.gnu.org
Subject: Re: bug#59531: 29.0.50: An alternative to `string-to-number` which
 throws
 an error (or returns a NIL value) when input is non-parseable as number
Date: Thu, 24 Nov 2022 10:00:06 +0200
tags 59531 wishlist
thanks

> Date: Thu, 24 Nov 2022 11:44:43 +0530
> From: Ramesh Nedunchezian <rameshnedunchezian <at> outlook.com>
> 
> `string-to-number` returns ZERO if the input is not a number.  
> 
> 
> This return value is not very helpful.  The choice of a number ZERO as "Not A Number" doesn't help one to distinguish between the following two cases
> 
> (1) Input was a valid number, and it parses to number zero
> 
> (2) Input was NOT a valid number, and it was forcibly reported as ZERO
> 
> Consider amending `string-to-number` to throw an error (or return NIL) when the input is not parseable as a number, or providing an alternative API to validate numbers.  I am trying to parse some fields in an org table, and see if the field value is a number or not;

Thanks.

Changing the default behavior to signal an error is out of the question,
since this is used in the Lisp reader and elsewhere, all over the place.  It
is very useful there.

However, as an enhancement, we could have an additional optional argument to
request that the function signal an error if the string cannot be parsed as
a number.

> If there is already an alternative to what I am trying to accomplish, I would appreciate a recipe.

You could match the string to a regexp that validates its numerical
appearance, before calling string-to-number.




Severity set to 'wishlist' from 'normal' Request was from Eli Zaretskii <eliz <at> gnu.org> to control <at> debbugs.gnu.org. (Thu, 24 Nov 2022 08:06:01 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#59531; Package emacs. (Thu, 24 Nov 2022 10:33:02 GMT) Full text and rfc822 format available.

Message #13 received at 59531 <at> debbugs.gnu.org (full text, mbox):

From: Jean Louis <bugs <at> gnu.support>
To: Ramesh Nedunchezian <rameshnedunchezian <at> outlook.com>
Cc: 59531 <at> debbugs.gnu.org
Subject: Re: bug#59531: 29.0.50: An alternative to `string-to-number` which
 throws an error (or returns a NIL value) when input is non-parseable as
 number
Date: Thu, 24 Nov 2022 13:08:20 +0300
* Ramesh Nedunchezian <rameshnedunchezian <at> outlook.com> [2022-11-24 09:22]:
> `string-to-number` returns ZERO if the input is not a number.  
> 
> 
> This return value is not very helpful.  The choice of a number ZERO as "Not A Number" doesn't help one to distinguish between the following two cases
> 
> (1) Input was a valid number, and it parses to number zero
> 
> (2) Input was NOT a valid number, and it was forcibly reported as ZERO
> 
> Consider amending `string-to-number` to throw an error (or return NIL) when the input is not parseable as a number, or providing an alternative API to validate numbers.  I am trying to parse some fields in an org table, and see if the field value is a number or not;
> 
> If there is already an alternative to what I am trying to
> accomplish, I would appreciate a recipe.

I had the same problem, so I have solved it this way.

(defun string-is-number-p (s)
  "Return number only if string is actual number, otherwise NIL."
  (let* ((s (string-trim s)))
    (cond ((seq-empty-p s) nil)
	  ((string-match "[^0123456789\\.-]" s) nil)
	  ((string-match "-" s 1) nil)
	  ((numberp (string-to-number s)) (string-to-number s)))))

As in my case I liketo know that string is really representing number
and nothing else.

(string-is-number-p "Hello") ➜ nil
(string-is-number-p "") ➜ nil
(string-is-number-p "0") ➜ 0
(string-is-number-p "0a") ➜ nil
(string-to-number "0a") ➜ 0
(string-is-number-p "0.1") ➜ 0.1
(string-is-number-p "-2.5121212") ➜ -2.5121212

My function may not be perfect.

--
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#59531; Package emacs. (Wed, 14 Dec 2022 16:41:02 GMT) Full text and rfc822 format available.

Message #16 received at 59531 <at> debbugs.gnu.org (full text, mbox):

From: Robert Pluim <rpluim <at> gmail.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 59531 <at> debbugs.gnu.org, Ramesh Nedunchezian <rameshnedunchezian <at> outlook.com>
Subject: Re: bug#59531: 29.0.50: An alternative to `string-to-number` which
 throws an error (or returns a NIL value) when input is non-parseable as
 number
Date: Wed, 14 Dec 2022 17:40:24 +0100
>>>>> On Thu, 24 Nov 2022 10:00:06 +0200, Eli Zaretskii <eliz <at> gnu.org> said:

    >> This return value is not very helpful.  The choice of a number ZERO
    >> as "Not A Number" doesn't help one to distinguish between the
    >> following two cases
    >> 
    >> (1) Input was a valid number, and it parses to number zero
    >> 
    >> (2) Input was NOT a valid number, and it was forcibly reported as ZERO
    >> 
    >> Consider amending `string-to-number` to throw an error (or return
    >> NIL) when the input is not parseable as a number, or providing an
    >> alternative API to validate numbers.  I am trying to parse some
    >> fields in an org table, and see if the field value is a number or
    >> not;

    Eli> Thanks.

    Eli> Changing the default behavior to signal an error is out of the question,
    Eli> since this is used in the Lisp reader and elsewhere, all over the place.  It
    Eli> is very useful there.

    Eli> However, as an enhancement, we could have an additional optional argument to
    Eli> request that the function signal an error if the string cannot be parsed as
    Eli> a number.

`cl-parse-integer' already has such an argument, but it only works for
integers. Alternatively, you could use `read-from-string' and check if
it returns something of type `integer' or `float'.

Robert
-- 




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#59531; Package emacs. (Fri, 16 Dec 2022 06:30:02 GMT) Full text and rfc822 format available.

Message #19 received at 59531 <at> debbugs.gnu.org (full text, mbox):

From: Jean Louis <bugs <at> gnu.support>
To: Robert Pluim <rpluim <at> gmail.com>
Cc: Eli Zaretskii <eliz <at> gnu.org>, 59531 <at> debbugs.gnu.org,
 Ramesh Nedunchezian <rameshnedunchezian <at> outlook.com>
Subject: Re: bug#59531: 29.0.50: An alternative to `string-to-number` which
 throws an error (or returns a NIL value) when input is non-parseable as
 number
Date: Fri, 16 Dec 2022 09:28:14 +0300
(defun string-is-number-p (s)
  "Return number only if string is actual number, otherwise NIL."
  (let* ((s (string-trim s)))
    (cond ((seq-empty-p s) nil)
	  ((string-match "[^0123456789\\.-]" s) nil)
	  ((string-match "-" s 1) nil)
	  ((numberp (string-to-number s)) (string-to-number s)))))


I use this function to make sure string is number and it
returns either the number of nil, and function is not
exhaustive.


Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/





This bug report was last modified 2 years and 185 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.