<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Precision and scale is getting changed in the dataframe while casting to decimal in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/precision-and-scale-is-getting-changed-in-the-dataframe-while/m-p/27345#M19222</link>
    <description>&lt;P&gt;You can use &lt;/P&gt;&lt;P&gt;typeof(COALESCE(Cast(3.45 as decimal(15,6)),0.0)); (instead of 0)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Sat, 13 Aug 2022 19:05:19 GMT</pubDate>
    <dc:creator>berserkersap</dc:creator>
    <dc:date>2022-08-13T19:05:19Z</dc:date>
    <item>
      <title>Precision and scale is getting changed in the dataframe while casting to decimal</title>
      <link>https://community.databricks.com/t5/data-engineering/precision-and-scale-is-getting-changed-in-the-dataframe-while/m-p/27339#M19216</link>
      <description>&lt;P&gt;When i run the below query in databricks sql the Precision and scale&amp;nbsp;of the decimal column is getting changed.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Select typeof(COALESCE(Cast(3.45 as decimal(15,6)),0));&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;o/p: decimal(16,6)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;expected o/p: decimal(15,6)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any reason why the Precision and scale is getting changed?&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 18:02:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/precision-and-scale-is-getting-changed-in-the-dataframe-while/m-p/27339#M19216</guid>
      <dc:creator>_Orc</dc:creator>
      <dc:date>2022-02-22T18:02:34Z</dc:date>
    </item>
    <item>
      <title>Re: Precision and scale is getting changed in the dataframe while casting to decimal</title>
      <link>https://community.databricks.com/t5/data-engineering/precision-and-scale-is-getting-changed-in-the-dataframe-while/m-p/27341#M19218</link>
      <description>&lt;P&gt;There is a reason indeed.&lt;/P&gt;&lt;P&gt;And the reason is type coercion:&lt;/P&gt;&lt;P&gt;In your coalesce, you enter 0 as a second value.  0 is an integer.&lt;/P&gt;&lt;P&gt;So Spark will coerce this to a decimal type.  For not losing any information, it needs 10 digits in front of the comma (max value of a signed integer is 2147483647 -&amp;gt; 10 digits).&lt;/P&gt;&lt;P&gt;So when you put (15,6) you only have 9 digits =&amp;gt; spark coerces this to 16,6.&lt;/P&gt;&lt;P&gt;The same for (8,0) =&amp;gt; 10,0&lt;/P&gt;&lt;P&gt;And (12,0) remains 12,0.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you want to avoid this coercion, you can pass 0.00 (decimal) instead of 0 (int).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I totally agree that the type coercion can be confusing btw.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Feb 2022 07:44:45 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/precision-and-scale-is-getting-changed-in-the-dataframe-while/m-p/27341#M19218</guid>
      <dc:creator>-werners-</dc:creator>
      <dc:date>2022-02-23T07:44:45Z</dc:date>
    </item>
    <item>
      <title>Re: Precision and scale is getting changed in the dataframe while casting to decimal</title>
      <link>https://community.databricks.com/t5/data-engineering/precision-and-scale-is-getting-changed-in-the-dataframe-while/m-p/27342#M19219</link>
      <description>&lt;P&gt;I believe the default precision and scale changes as you change the scale.&lt;/P&gt;&lt;P&gt;For instance if you set precision and scale for casting as (4,0), then spark will default it to (10,0).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For (8,1), it'll default to (11,1)&lt;/P&gt;&lt;P&gt;For (8,2), it'll default to (12,2)&lt;/P&gt;&lt;P&gt;For (8,3), it'll default to (13,3)&lt;/P&gt;&lt;P&gt;For (8,4), it'll default to (14,4)&lt;/P&gt;&lt;P&gt;For (8,5), it'll default to (15,5)&lt;/P&gt;&lt;P&gt;For (8,6), it'll default to (16,6) and so on..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think the logic is:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;if precision-scale &amp;lt; 10:
    precision = 10+scale
else
    precision=precision&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Feb 2022 07:54:06 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/precision-and-scale-is-getting-changed-in-the-dataframe-while/m-p/27342#M19219</guid>
      <dc:creator>AmanSehgal</dc:creator>
      <dc:date>2022-02-23T07:54:06Z</dc:date>
    </item>
    <item>
      <title>Re: Precision and scale is getting changed in the dataframe while casting to decimal</title>
      <link>https://community.databricks.com/t5/data-engineering/precision-and-scale-is-getting-changed-in-the-dataframe-while/m-p/27343#M19220</link>
      <description>&lt;P&gt;This is only correct if you pass an integer as 2nd argument in the coalesce.&lt;/P&gt;&lt;P&gt;F.e. Select typeof(COALESCE(Cast(3.45 as decimal(11,2)),cast(0 as long))) returns (22,2) because long requires 20 digits in front of the comma.&lt;/P&gt;&lt;P&gt;If you pass a decimal, other rules apply.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Feb 2022 08:18:08 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/precision-and-scale-is-getting-changed-in-the-dataframe-while/m-p/27343#M19220</guid>
      <dc:creator>-werners-</dc:creator>
      <dc:date>2022-02-23T08:18:08Z</dc:date>
    </item>
    <item>
      <title>Re: Precision and scale is getting changed in the dataframe while casting to decimal</title>
      <link>https://community.databricks.com/t5/data-engineering/precision-and-scale-is-getting-changed-in-the-dataframe-while/m-p/27344#M19221</link>
      <description>&lt;P&gt;Hi @Om Singh​&amp;nbsp; - You can try below, Second statement will give you the required result&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;%sql
Select typeof(COALESCE(Cast(3.45 as decimal(15,6)),0)) as CastToDecimal
Union All
Select typeof(Cast(COALESCE(3.45,0) as decimal(15,6))) as CastToDecimal;
&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;     Result of above:&lt;/P&gt;&lt;P&gt;     &lt;B&gt;&amp;nbsp;&lt;/B&gt;&lt;/P&gt;&lt;P&gt;&lt;B&gt;CastToDecimal&lt;/B&gt;&lt;/P&gt;&lt;P&gt;&lt;B&gt;1   &lt;/B&gt;decimal(16,6)&lt;/P&gt;&lt;P&gt;&lt;B&gt;2  &lt;/B&gt;decimal(15,6)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Feb 2022 14:21:06 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/precision-and-scale-is-getting-changed-in-the-dataframe-while/m-p/27344#M19221</guid>
      <dc:creator>RKNutalapati</dc:creator>
      <dc:date>2022-02-23T14:21:06Z</dc:date>
    </item>
    <item>
      <title>Re: Precision and scale is getting changed in the dataframe while casting to decimal</title>
      <link>https://community.databricks.com/t5/data-engineering/precision-and-scale-is-getting-changed-in-the-dataframe-while/m-p/27345#M19222</link>
      <description>&lt;P&gt;You can use &lt;/P&gt;&lt;P&gt;typeof(COALESCE(Cast(3.45 as decimal(15,6)),0.0)); (instead of 0)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 13 Aug 2022 19:05:19 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/precision-and-scale-is-getting-changed-in-the-dataframe-while/m-p/27345#M19222</guid>
      <dc:creator>berserkersap</dc:creator>
      <dc:date>2022-08-13T19:05:19Z</dc:date>
    </item>
  </channel>
</rss>

