{"id":26005,"date":"2022-06-29T11:32:03","date_gmt":"2022-06-29T01:32:03","guid":{"rendered":"https:\/\/database.guide\/?p=26005"},"modified":"2022-06-29T11:32:45","modified_gmt":"2022-06-29T01:32:45","slug":"redis-incrbyfloat-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-incrbyfloat-command-explained\/","title":{"rendered":"Redis INCRBYFLOAT Command Explained"},"content":{"rendered":"\n<p>In Redis, the <code>INCRBYFLOAT<\/code> command increments a floating point number by the specified amount. More specifically, it increments the string representing a floating point number stored at the specified key.<\/p>\n\n\n\n<p>If the key doesn&#8217;t exist, <code>INCRBYFLOAT<\/code> creates the key with a value of <code>0<\/code> and then increments it by the specified amount.<\/p>\n\n\n\n<p>An error occurs if the key contains a value of the wrong type, or if the current key content or the specified increment are not parsable as a double precision floating point number.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>INCRBYFLOAT key increment<\/code><\/pre>\n\n\n\n<p>Where <code><em>key<\/em><\/code> is the key to increment, and <em><code>increment<\/code><\/em> is the amount to increment it by.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Suppose we set a key like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET balance 1.58<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">OK<\/pre>\n\n\n\n<p>Let&#8217;s quickly take a look at it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET balance<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"1.58\"<\/pre>\n\n\n\n<p>OK, now let&#8217;s increment it by a floating point :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCRBYFLOAT balance 0.1<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"1.68\"<\/pre>\n\n\n\n<p><code>INCRBYFLOAT<\/code> returns a bulk string reply with the new value of the key after it&#8217;s been incremented.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Negative Values<\/h2>\n\n\n\n<p>We can increment the value by a negative amount just as we can increment positive values. In other words, we can decrement the value by using a negative value.<\/p>\n\n\n\n<p>Set a negative value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCRBYFLOAT balance -10.675<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"-8.995\"<\/pre>\n\n\n\n<p>In this case, it resulted is a negative value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When the Key Doesn&#8217;t Exist<\/h2>\n\n\n\n<p>If the key doesn&#8217;t exist, <code>INCRBYFLOAT<\/code> creates the key with a value of <code>0<\/code> and then increments it by the specified amount.<\/p>\n\n\n\n<p>Let&#8217;s delete our <code>balance<\/code> key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DEL balance<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 1<\/pre>\n\n\n\n<p>The <code>DEL<\/code> command returns the number of keys that were deleted &#8211; in this case it&#8217;s 1.<\/p>\n\n\n\n<p>Now let&#8217;s try to increment that (non-existent) key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCRBYFLOAT balance 1.12345678912345678<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"1.12345678912345678\"<\/pre>\n\n\n\n<p>So we can see that <code>balance<\/code> has been created again, this time with a value that&#8217;s the same that we incremented it by. <\/p>\n\n\n\n<p>The precision of the output is fixed at 17 digits after the decimal point regardless of the actual internal precision of the computation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When the Value is the Wrong Type<\/h2>\n\n\n\n<p>An error occurs if the key contains a value of the wrong type or contains a string that cannot be represented as a floating point number.<\/p>\n\n\n\n<p>Let&#8217;s test this by trying to increment a value that&#8217;s not a valid float.<\/p>\n\n\n\n<p>Set the amount:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET balance \"Ten\"<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">OK<\/pre>\n\n\n\n<p>Let&#8217;s take a look at it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET balance<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"Ten\"<\/pre>\n\n\n\n<p>OK, now let&#8217;s try to increment it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCRBYFLOAT balance 1.5<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(error) ERR value is not a valid float<\/pre>\n\n\n\n<p>An error occurred as expected.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Similar Commands<\/h2>\n\n\n\n<p>We can also use <code><a href=\"https:\/\/database.guide\/redis-incr-command-explained\/\" data-type=\"post\" data-id=\"25959\">INCR<\/a><\/code> to increment by one, and <code><a href=\"https:\/\/database.guide\/redis-incrby-command-explained\/\" data-type=\"post\" data-id=\"25970\">INCRBY<\/a><\/code> to increment by an integer amount. <\/p>\n\n\n\n<p>Example of <code>INCR<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCR balance<\/code><\/pre>\n\n\n\n<p>Example of <code>INCRBY<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCRBY balance 2<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Redis, the INCRBYFLOAT command increments a floating point number by the specified amount. More specifically, it increments the string representing a floating point number stored at the specified key. If the key doesn&#8217;t exist, INCRBYFLOAT creates the key with a value of 0 and then increments it by the specified amount. An error occurs &#8230; <a title=\"Redis INCRBYFLOAT Command Explained\" class=\"read-more\" href=\"https:\/\/database.guide\/redis-incrbyfloat-command-explained\/\" aria-label=\"Read more about Redis INCRBYFLOAT Command Explained\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[154,84,20],"class_list":["post-26005","post","type-post","status-publish","format-standard","hentry","category-redis","tag-commands","tag-string","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=26005"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26005\/revisions"}],"predecessor-version":[{"id":26287,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26005\/revisions\/26287"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=26005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=26005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=26005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}