440,537 Members | 1,471 Online
Need help? Post your question and get tips & solutions from a community of 440,537 IT Pros & Developers. It's quick & easy.

# how to get nr of fraction digits

 P: n/a Hello , Does someone knows a simple way of how to get the nr of fraction digits ? example : 1.23 would give a result of 2 1.234 would give a result of 3 Yes I know i could split the number on the decimal seperator and then count the characters in the second part of the resulting array , but i thought ,, there should be another solution ? the task should be simple but i am looking in all sorts of system math classes but i am not seeing it ... or should i go for the split method ? :-) regards Michel Sep 10 '07 #1
17 Replies

 P: n/a What could be simpler than .ToString.Split("."c)(1).Length? What makes you think that there 'should' be another solution? "Michel Posseth [MCP]"

 P: n/a Hello Stephany , well Dim decval As Decimal = CDec(1.23) MsgBox(decval.ToString.Split(CChar(System.Globaliz ation.NumberFormatInfo.CurrentInfo.NumberDecimalSe parator))(1).Length()) works fine however , i should also check for exceptions that might occur i just thought that there would be a built in method for this seemingly common and "simple" task Michel Posseth "Stephany Young" wrote: What could be simpler than .ToString.Split("."c)(1).Length? What makes you think that there 'should' be another solution? "Michel Posseth [MCP]"

 P: n/a Nobody ?? Nobody who knows a different way of doing this ? "Michel Posseth [MCP]" What could be simpler than .ToString.Split("."c)(1).Length?What makes you think that there 'should' be another solution? "Michel Posseth [MCP]"

 P: n/a On Sep 10, 12:37 pm, "Michel Posseth [MCP]" .ToString.Split("."c)(1).Length? What makes you think that there 'should' be another solution? "Michel Posseth [MCP]"

 P: n/a "rowe_newsgroups" Nobody ??Nobody who knows a different way of doing this ?"Michel Posseth [MCP]" schreefin berichtnews:87**********************************@m icrosoft.com... Hello Stephany , well Dim decval As Decimal = CDec(1.23) MsgBox(decval.ToString.Split(CChar(System.Globaliz ation.NumberFormatInfo.CurrentInfo.NumberDecimalSe parator))(1).Length()) works fine however , i should also check for exceptions that might occur i just thought that there would be a built in method for this seemingly common and "simple" task Michel Posseth "Stephany Young" wrote: >What could be simpler than .ToString.Split("."c)(1).Length? >What makes you think that there 'should' be another solution? >"Michel Posseth [MCP]" wroteinmessagenews:62**********************************@ microsoft.com... Hello , Does someone knows a simple way of how to get the nr of fraction digits ? example : 1.23 would give a result of 2 1.234 would give a result of 3 Yes I know i could split the number on the decimal seperator and then count the characters in the second part of the resulting array , but i thought ,, there should be another solution ? the task should be simple but i am looking in all sorts of system math classes but i am not seeing it ... or should i go for the split method ? :-) regards Michel Maybe I'm missing something, but why do you need to do this and why are you against using Split? Are you worried about performance of creating an array or do you just want a more "mathematical" way of doing it? Thanks, Seth Rowe The reasson is the following , i am currently employed at a energy company as you understand every sort meter has a different counter depending on the sort of customer consumer , company and / or the sort of energy electricity / Gas etc etc So when we use reporting tools we need to know on forehand how manny digits after the decimal can be displayed by the meter as we are talking about a few million counters in these meters i am afraid that the string thingy might degrade the preformance that is one and second is indeed that i expected a more generic way as Private Function DigitsAfterDecimal(ByVal Decval As Decimal) As Integer Dim SplitArr() As String = Decval.ToString.Split(CChar(System.Globalization.N umberFormatInfo.CurrentInfo.NumberDecimalSeparator )) If SplitArr.Length = 2 Then Return SplitArr(1).Length Else Return 0 End If End Function I honestly expected a framework method for this , one that i missed in my quest , but as before it seems like i am the only one with such business tasks as i have , so for all of you outta there above is the best perfomer i ended up with Sep 10 '07 #6

 P: n/a Though I'm not really well versed in mathematics but if I remember Log10 gives the the number of digits of an integer (i.e. taking the decimal part and using Log10 should give the result you are looking for). Make sure to check this with a math site.... --- Patrice "Michel Posseth [MCP]" schreef in bericht news:87**********************************@microsof t.com... >Hello Stephany ,wellDim decval As Decimal = CDec(1.23)MsgBox(decval.ToString.Split(CChar(System.Globali zation.NumberFormatInfo.CurrentInfo.NumberDecimalS eparator))(1).Length())works fine however , i should also check for exceptions that might occuri just thought that there would be a built in method for this seeminglycommon and "simple" taskMichel Posseth "Stephany Young" wrote: >>What could be simpler than .ToString.Split("."c)(1).Length?What makes you think that there 'should' be another solution? "Michel Posseth [MCP]" wrote inmessage news:62**********************************@microsof t.com...Hello ,Does someone knows a simple way of how to get the nr of fractiondigits?example :1.23 would give a result of 21.234 would give a result of 3 YesI know i could split the number on the decimal seperator and thencountthecharacters in the second part of the resulting array , but i thought,,thereshould be another solution ?the task should be simple but i am looking in all sorts of system mathclasses but i am not seeing it ...or should i go for the split method ? :-) regardsMichel Sep 10 '07 #7

 P: n/a On Sep 10, 1:32 pm, "Patrice"

 P: n/a Think this only works on integers, so you would still have to split off the decimal part, know its length (so you knew what power of ten to multiply it by) and then call this method. So I think the before mentioned solution is the best. -- Terry "rowe_newsgroups" wrote: On Sep 10, 1:32 pm, "Patrice"

 P: n/a "rowe_newsgroups" Though I'm not really well versed in mathematics but if I remember Log10gives the the number of digits of an integer (i.e. taking the decimalpartand using Log10 should give the result you are looking for).Make sure to check this with a math site.... Doing some quick googling it seems that with a little help form ABS and Floor Log10 will do the job. Here's a neat C++ article that has the formula (don't worry it's easy to read - and should be an easy port to vb). http://www.gamedev.net/community/for...opic_id=309809 You'll have to run some speed tests between string manipulation and using the math based approach - I'm curious as to which is more performant. Thanks, Seth Rowe The code you pointed to shows how to count the numbers of the whole digit , i am only interested in the numbers behind the decimal seperator so 1.234 = 3 numbers behind the seperator 1 = 0 numbers behind the seperator to be more clear 123445.2 = 1 number behind the seperator :-) well i guess , i will implement the method i showed above , guess there is no other way of doing this but anyway thanks for your attention Regards Michel Sep 10 '07 #10

 P: n/a Michel Posseth [MCP] wrote: >Maybe I'm missing something, but why do you need to do this and whyare you against using Split? Are you worried about performance ofcreating an array or do you just want a more "mathematical" way ofdoing it?Thanks,Seth Rowe The reasson is the following , i am currently employed at a energy company as you understand every sort meter has a different counter depending on the sort of customer consumer , company and / or the sort of energy electricity / Gas etc etc So when we use reporting tools we need to know on forehand how manny digits after the decimal can be displayed by the meter as we are talking about a few million counters in these meters i am afraid that the string thingy might degrade the preformance that is one and second is indeed that i expected a more generic way as Private Function DigitsAfterDecimal(ByVal Decval As Decimal) As Integer Dim SplitArr() As String = Decval.ToString.Split(CChar(System.Globalization.N umberFormatInfo.CurrentInfo.NumberDecimalSeparator )) If SplitArr.Length = 2 Then Return SplitArr(1).Length Else Return 0 End If End Function I honestly expected a framework method for this , one that i missed in my quest , but as before it seems like i am the only one with such business tasks as i have , so for all of you outta there above is the best perfomer i ended up with This isn't really a mathematical problem, as you are asking for the number of fractional digits when the number is represented as a string. Floating point values are not stored as decimal values, so the values that you see might not be the exact value, due to the limited precision of floating point values. When you see the value 1.23, it might actually be stored as the value 1.22999999999997 as that may be the closest possible value that can be represented as a floating point value. The values are rounded when displayed, so you will usually never see these small differences, but they are basically the reason why there is no built-in method to get the number of fractional digits. If you want a string method that performs a little bit better, use the IndexOf method to locate the decimal separator, that way you can just count the number of characters without creating an array of strings: Dim value As String = Decval.ToString(); Dim pos As Integer = value.IndexOf(System.Globalization.NumberFormatInf o.CurrentInfo.NumberDecimalSeparator) If pos == -1 Then Return 0 Else Return value.Length - pos - 1 End If -- Göran Andersson _____ http://www.guffa.com Sep 10 '07 #11

 P: n/a I don't think the OP specified if the input starts out as a string or not, but I assume it is a string. It seems to me that any manipulation using floating point numbers is going to run into the inability to represent some fractional base 10 numbers exactly in base 2. On Mon, 10 Sep 2007 19:32:11 +0200, "Patrice" Though I'm not really well versed in mathematics but if I remember Log10gives the the number of digits of an integer (i.e. taking the decimal partand using Log10 should give the result you are looking for).Make sure to check this with a math site....---Patrice "Michel Posseth [MCP]" Nobody ??Nobody who knows a different way of doing this ? "Michel Posseth [MCP]" schreef in berichtnews:87**********************************@microso ft.com... >>Hello Stephany ,wellDim decval As Decimal = CDec(1.23)MsgBox(decval.ToString.Split(CChar(System.Global ization.NumberFormatInfo.CurrentInfo.NumberDecimal Separator))(1).Length())works fine however , i should also check for exceptions that might occuri just thought that there would be a built in method for this seeminglycommon and "simple" taskMichel Posseth "Stephany Young" wrote:What could be simpler than .ToString.Split("."c)(1).Length?What makes you think that there 'should' be another solution? "Michel Posseth [MCP]" wrote inmessage news:62**********************************@microsof t.com...Hello ,Does someone knows a simple way of how to get the nr of fractiondigits?example :1.23 would give a result of 21.234 would give a result of 3 YesI know i could split the number on the decimal seperator and thencountthecharacters in the second part of the resulting array , but i thought,,thereshould be another solution ?the task should be simple but i am looking in all sorts of system mathclasses but i am not seeing it ...or should i go for the split method ? :-) regardsMichel Sep 10 '07 #12

 P: n/a "Göran Andersson" >Maybe I'm missing something, but why do you need to do this and whyare you against using Split? Are you worried about performance ofcreating an array or do you just want a more "mathematical" way ofdoing it?Thanks,Seth Rowe The reasson is the following ,i am currently employed at a energy company as you understand every sortmeter has a different counter depending on the sort of customer consumer, company and / or the sort of energy electricity / Gas etc etcSo when we use reporting tools we need to know on forehand how mannydigits after the decimal can be displayed by the meter as we are talking about a few million counters in these meters i amafraid that the string thingy might degrade the preformance that is oneand second is indeed that i expected a more generic way as Private Function DigitsAfterDecimal(ByVal Decval As Decimal) As IntegerDim SplitArr() As String =Decval.ToString.Split(CChar(System.Globalization. NumberFormatInfo.CurrentInfo.NumberDecimalSeparato r))If SplitArr.Length = 2 ThenReturn SplitArr(1).LengthElseReturn 0End IfEnd Function I honestly expected a framework method for this , one that i missed in myquest , but as before it seems like i am the only one with such businesstasks as i have , so for all of you outta there above is the bestperfomer i ended up with This isn't really a mathematical problem, as you are asking for the number of fractional digits when the number is represented as a string. Floating point values are not stored as decimal values, so the values that you see might not be the exact value, due to the limited precision of floating point values. When you see the value 1.23, it might actually be stored as the value 1.22999999999997 as that may be the closest possible value that can be represented as a floating point value. The values are rounded when displayed, so you will usually never see these small differences, but they are basically the reason why there is no built-in method to get the number of fractional digits. If you want a string method that performs a little bit better, use the IndexOf method to locate the decimal separator, that way you can just count the number of characters without creating an array of strings: Dim value As String = Decval.ToString(); Dim pos As Integer = value.IndexOf(System.Globalization.NumberFormatInf o.CurrentInfo.NumberDecimalSeparator) If pos == -1 Then Return 0 Else Return value.Length - pos - 1 End If -- Göran Andersson _____ http://www.guffa.com This perfoms indeed a lot bether in my initial tests Private Function DigitsafterDecimal(ByVal DecVal As Decimal) As Integer Dim value As String = DecVal.ToString() Dim pos As Integer = value.IndexOf(System.Globalization.NumberFormatInf o.CurrentInfo.NumberDecimalSeparator) If pos = -1 Then Return 0 Else Return value.Length - pos - 1 End If End Function Thank you verry much michel Sep 10 '07 #13

 P: n/a >I don't think the OP specified if the input starts out as a string or not, but I assume it is a string. No ,, the the input is a Decimal coming from a SQL database I guess Göran Andersson understood exactly where i went wrong in my thoughts that there should be a mathmetical function that i overlooked so for anyone interested read his reply , For everyone who joined this discussion , thank you verry much for your input regards Michel Posseth "Jack Jackson" I don't think the OP specified if the input starts out as a string or not, but I assume it is a string. It seems to me that any manipulation using floating point numbers is going to run into the inability to represent some fractional base 10 numbers exactly in base 2. On Mon, 10 Sep 2007 19:32:11 +0200, "Patrice" >Though I'm not really well versed in mathematics but if I remember Log10gives the the number of digits of an integer (i.e. taking the decimal partand using Log10 should give the result you are looking for).Make sure to check this with a math site....---Patrice "Michel Posseth [MCP]" >Nobody ??Nobody who knows a different way of doing this ? "Michel Posseth [MCP]" schreef in berichtnews:87**********************************@micros oft.com...Hello Stephany ,wellDim decval As Decimal = CDec(1.23)MsgBox(decval.ToString.Split(CChar(System.Globa lization.NumberFormatInfo.CurrentInfo.NumberDecima lSeparator))(1).Length())works fine however , i should also check for exceptions that mightoccuri just thought that there would be a built in method for this seeminglycommon and "simple" taskMichel Posseth "Stephany Young" wrote:What could be simpler than .ToString.Split("."c)(1).Length?>What makes you think that there 'should' be another solution?>>>"Michel Posseth [MCP]" wrote inmessage news:62**********************************@microsof t.com...Hello ,Does someone knows a simple way of how to get the nr of fractiondigits?example :1.23 would give a result of 21.234 would give a result of 3 YesI know i could split the number on the decimal seperator and thencountthecharacters in the second part of the resulting array , but i thought,,thereshould be another solution ?the task should be simple but i am looking in all sorts of systemmathclasses but i am not seeing it ...or should i go for the split method ? :-) regardsMichel >> Sep 10 '07 #14

 P: n/a Goran, Really I find this a very nice answer, although I find the solution from Stephany better, I think that the one from Stephany will be the most optimal one, however, I did not try it. Cor "Göran Andersson" >Maybe I'm missing something, but why do you need to do this and whyare you against using Split? Are you worried about performance ofcreating an array or do you just want a more "mathematical" way ofdoing it?Thanks,Seth Rowe The reasson is the following ,i am currently employed at a energy company as you understand every sortmeter has a different counter depending on the sort of customer consumer, company and / or the sort of energy electricity / Gas etc etcSo when we use reporting tools we need to know on forehand how mannydigits after the decimal can be displayed by the meter as we are talking about a few million counters in these meters i amafraid that the string thingy might degrade the preformance that is oneand second is indeed that i expected a more generic way as Private Function DigitsAfterDecimal(ByVal Decval As Decimal) As IntegerDim SplitArr() As String =Decval.ToString.Split(CChar(System.Globalization. NumberFormatInfo.CurrentInfo.NumberDecimalSeparato r))If SplitArr.Length = 2 ThenReturn SplitArr(1).LengthElseReturn 0End IfEnd Function I honestly expected a framework method for this , one that i missed in myquest , but as before it seems like i am the only one with such businesstasks as i have , so for all of you outta there above is the bestperfomer i ended up with This isn't really a mathematical problem, as you are asking for the number of fractional digits when the number is represented as a string. Floating point values are not stored as decimal values, so the values that you see might not be the exact value, due to the limited precision of floating point values. When you see the value 1.23, it might actually be stored as the value 1.22999999999997 as that may be the closest possible value that can be represented as a floating point value. The values are rounded when displayed, so you will usually never see these small differences, but they are basically the reason why there is no built-in method to get the number of fractional digits. If you want a string method that performs a little bit better, use the IndexOf method to locate the decimal separator, that way you can just count the number of characters without creating an array of strings: Dim value As String = Decval.ToString(); Dim pos As Integer = value.IndexOf(System.Globalization.NumberFormatInf o.CurrentInfo.NumberDecimalSeparator) If pos == -1 Then Return 0 Else Return value.Length - pos - 1 End If -- Göran Andersson _____ http://www.guffa.com Sep 11 '07 #15

 P: n/a Cor, The string split solution is the most obvious way , however the way to go is not only .ToString.Split("."c)(1).Length? but is as a complete function Private Function DigitsAfterDecimal(ByVal Decval As Decimal) As Integer Dim SplitArr() As String = Decval.ToString.Split(CChar(System.Globalization.N umberFormatInfo.CurrentInfo.NumberDecimalSeparator )) If SplitArr.Length = 2 Then Return SplitArr(1).Length Else Return 0 End If End Function Cause what would else happen with digits that do not have a decimal suffix ? and in my situation this is a posibility . i have tested both functions and in complexity and readability they are the same however Goran`s example performs slightly bether not much but in a loop of a few thousands it is noticable. regards Michel "Cor Ligthert[MVP]" wrote: Goran, Really I find this a very nice answer, although I find the solution from Stephany better, I think that the one from Stephany will be the most optimal one, however, I did not try it. Cor "Gran Andersson" Maybe I'm missing something, but why do you need to do this and whyare you against using Split? Are you worried about performance ofcreating an array or do you just want a more "mathematical" way ofdoing it?Thanks,Seth Rowe The reasson is the following , i am currently employed at a energy company as you understand every sort meter has a different counter depending on the sort of customer consumer , company and / or the sort of energy electricity / Gas etc etc So when we use reporting tools we need to know on forehand how manny digits after the decimal can be displayed by the meter as we are talking about a few million counters in these meters i am afraid that the string thingy might degrade the preformance that is one and second is indeed that i expected a more generic way as Private Function DigitsAfterDecimal(ByVal Decval As Decimal) As Integer Dim SplitArr() As String = Decval.ToString.Split(CChar(System.Globalization.N umberFormatInfo.CurrentInfo.NumberDecimalSeparator )) If SplitArr.Length = 2 Then Return SplitArr(1).Length Else Return 0 End If End Function I honestly expected a framework method for this , one that i missed in my quest , but as before it seems like i am the only one with such business tasks as i have , so for all of you outta there above is the best perfomer i ended up with This isn't really a mathematical problem, as you are asking for the number of fractional digits when the number is represented as a string. Floating point values are not stored as decimal values, so the values that you see might not be the exact value, due to the limited precision of floating point values. When you see the value 1.23, it might actually be stored as the value 1.22999999999997 as that may be the closest possible value that can be represented as a floating point value. The values are rounded when displayed, so you will usually never see these small differences, but they are basically the reason why there is no built-in method to get the number of fractional digits. If you want a string method that performs a little bit better, use the IndexOf method to locate the decimal separator, that way you can just count the number of characters without creating an array of strings: Dim value As String = Decval.ToString(); Dim pos As Integer = value.IndexOf(System.Globalization.NumberFormatInf o.CurrentInfo.NumberDecimalSeparator) If pos == -1 Then Return 0 Else Return value.Length - pos - 1 End If -- Gran Andersson _____ http://www.guffa.com Sep 11 '07 #16

 P: n/a Michel, You are right the code from Goran is better then in the way Stephany has showed, however what is good does not need changes and that was more my intention of the message. Afterwards I though that will not be understood. Probably can you even improve the code that Goran supported if you can make from the search string (your decimal point which returns a string) a char. In past we have done a test in this newsgroup, where the indexOf from a char was the quickest, followed by a VisualBasic method and then the IndexOf string. The split was very slow for this. (The code from Goran beside of course that ==) :-) Cor "Michel Posseth [MCP]" .ToString.Split("."c)(1).Length? but is as a complete function Private Function DigitsAfterDecimal(ByVal Decval As Decimal) As Integer Dim SplitArr() As String = Decval.ToString.Split(CChar(System.Globalization.N umberFormatInfo.CurrentInfo.NumberDecimalSeparator )) If SplitArr.Length = 2 Then Return SplitArr(1).Length Else Return 0 End If End Function Cause what would else happen with digits that do not have a decimal suffix ? and in my situation this is a posibility . i have tested both functions and in complexity and readability they are the same however Goran`s example performs slightly bether not much but in a loop of a few thousands it is noticable. regards Michel "Cor Ligthert[MVP]" wrote: >Goran,Really I find this a very nice answer, although I find the solution fromStephany better, I think that the one from Stephany will be the mostoptimalone, however, I did not try it.Cor"Gran Andersson"

 P: n/a Cor Ligthert[MVP] wrote: Michel, You are right the code from Goran is better then in the way Stephany has showed, however what is good does not need changes and that was more my intention of the message. Afterwards I though that will not be understood. Probably can you even improve the code that Goran supported if you can make from the search string (your decimal point which returns a string) a char. In past we have done a test in this newsgroup, where the indexOf from a char was the quickest, followed by a VisualBasic method and then the IndexOf string. The split was very slow for this. (The code from Goran beside of course that ==) :-) Cor Converting the string to a char does actually increase performance: Dim pos As Integer = value.IndexOf(CChar(System.Globalization.NumberFor matInfo.CurrentInfo.NumberDecimalSeparator)) With this, I measured a 35% better performance than splitting the string. :) -- GĂ¶ran Andersson _____ http://www.guffa.com Sep 11 '07 #18

### This discussion thread is closed

Replies have been disabled for this discussion.