By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,537 Members | 1,471 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
17 Replies


P: n/a
What could be simpler than <variable>.ToString.Split("."c)(1).Length?

What makes you think that there 'should' be another solution?

"Michel Posseth [MCP]" <Mi**************@discussions.microsoft.comwrote in
message news:62**********************************@microsof t.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

Sep 10 '07 #2

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 <variable>.ToString.Split("."c)(1).Length?

What makes you think that there 'should' be another solution?

"Michel Posseth [MCP]" <Mi**************@discussions.microsoft.comwrote in
message news:62**********************************@microsof t.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

Sep 10 '07 #3

P: n/a
Nobody ??

Nobody who knows a different way of doing this ?
"Michel Posseth [MCP]" <Mi**************@discussions.microsoft.comschre ef
in bericht news:87**********************************@microsof t.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 <variable>.ToString.Split("."c)(1).Length?

What makes you think that there 'should' be another solution?

"Michel Posseth [MCP]" <Mi**************@discussions.microsoft.comwrote
in
message news:62**********************************@microsof t.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



Sep 10 '07 #4

P: n/a
On Sep 10, 12:37 pm, "Michel Posseth [MCP]" <M...@posseth.comwrote:
Nobody ??

Nobody who knows a different way of doing this ?

"Michel Posseth [MCP]" <MichelPosseth...@discussions.microsoft.comschre ef
in 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 <variable>.ToString.Split("."c)(1).Length?
What makes you think that there 'should' be another solution?
"Michel Posseth [MCP]" <MichelPosseth...@discussions.microsoft.comwrote
in
messagenews:62**********************************@m icrosoft.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

Sep 10 '07 #5

P: n/a

"rowe_newsgroups" <ro********@yahoo.comschreef in bericht
news:11**********************@r34g2000hsd.googlegr oups.com...
On Sep 10, 12:37 pm, "Michel Posseth [MCP]" <M...@posseth.comwrote:
>Nobody ??

Nobody who knows a different way of doing this ?

"Michel Posseth [MCP]" <MichelPosseth...@discussions.microsoft.com>
schreef
in 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 <variable>.ToString.Split("."c)(1).Length?
>What makes you think that there 'should' be another solution?
>"Michel Posseth [MCP]" <MichelPosseth...@discussions.microsoft.com>
wrote
in
messagenews: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]" <MS**@posseth.coma écrit dans le message de news:
ey**************@TK2MSFTNGP06.phx.gbl...
Nobody ??

Nobody who knows a different way of doing this ?
"Michel Posseth [MCP]" <Mi**************@discussions.microsoft.com>
schreef in bericht
news:87**********************************@microsof t.com...
>Hello Stephany ,

well

Dim 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 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 <variable>.ToString.Split("."c)(1).Length?

What makes you think that there 'should' be another solution?

"Michel Posseth [MCP]" <Mi**************@discussions.microsoft.com>
wrote in
message news:62**********************************@microsof t.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



Sep 10 '07 #7

P: n/a
On Sep 10, 1:32 pm, "Patrice" <http://www.chez.com/scribe/wrote:
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....
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

Sep 10 '07 #8

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" <http://www.chez.com/scribe/wrote:
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....

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

Sep 10 '07 #9

P: n/a

"rowe_newsgroups" <ro********@yahoo.comschreef in bericht
news:11*********************@r34g2000hsd.googlegro ups.com...
On Sep 10, 1:32 pm, "Patrice" <http://www.chez.com/scribe/wrote:
>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....

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 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
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"
<http://www.chez.com/scribe/wrote:
>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]" <MS**@posseth.coma écrit dans le message de news:
ey**************@TK2MSFTNGP06.phx.gbl...
>Nobody ??

Nobody who knows a different way of doing this ?
"Michel Posseth [MCP]" <Mi**************@discussions.microsoft.com>
schreef in bericht
news:87**********************************@microso ft.com...
>>Hello Stephany ,

well

Dim 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 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 <variable>.ToString.Split("."c)(1).Length?

What makes you think that there 'should' be another solution?

"Michel Posseth [MCP]" <Mi**************@discussions.microsoft.com>
wrote in
message news:62**********************************@microsof t.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


Sep 10 '07 #12

P: n/a

"Göran Andersson" <gu***@guffa.comschreef in bericht
news:%2****************@TK2MSFTNGP04.phx.gbl...
Michel Posseth [MCP] wrote:
>>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. NumberFormatInfo.CurrentInfo.NumberDecimalSeparato r))

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


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" <ja********@pebbleridge.comschreef in bericht
news:jn********************************@4ax.com...
>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"
<http://www.chez.com/scribe/wrote:
>>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]" <MS**@posseth.coma écrit dans le message de news:
ey**************@TK2MSFTNGP06.phx.gbl...
>>Nobody ??

Nobody who knows a different way of doing this ?
"Michel Posseth [MCP]" <Mi**************@discussions.microsoft.com>
schreef in bericht
news:87**********************************@micros oft.com...
Hello Stephany ,

well

Dim 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 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 <variable>.ToString.Split("."c)(1).Length?
>
What makes you think that there 'should' be another solution?
>
>
>
"Michel Posseth [MCP]" <Mi**************@discussions.microsoft.com>
wrote in
message news:62**********************************@microsof t.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
>
>


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" <gu***@guffa.comschreef in bericht
news:%2****************@TK2MSFTNGP04.phx.gbl...
Michel Posseth [MCP] wrote:
>>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. NumberFormatInfo.CurrentInfo.NumberDecimalSeparato r))

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 11 '07 #15

P: n/a
Cor,
The string split solution is the most obvious way , however the way to go is
not only

<variable>.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" <gu***@guffa.comschreef in bericht
news:%2****************@TK2MSFTNGP04.phx.gbl...
Michel Posseth [MCP] wrote:
>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
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]" <Mi**************@discussions.microsoft.comschre ef
in bericht news:D0**********************************@microsof t.com...
Cor,
The string split solution is the most obvious way , however the way to go
is
not only

<variable>.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" <gu***@guffa.comschreef in bericht
news:%2****************@TK2MSFTNGP04.phx.gbl...
Michel Posseth [MCP] wrote:
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. NumberFormatInfo.CurrentInfo.NumberDecimalSeparato r))

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 #17

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.