how to get nr of fraction digits

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
What could be simpler than <variable>.ToString.Split("."c)(1).Length?

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

well

Dim decval As Decimal = CDec(1.23)

MsgBox(decval.ToString.Split(CChar(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator))(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
 
Nobody ??

Nobody who knows a different way of doing this ?

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
 
rowe_newsgroups said:
Nobody ??

Nobody who knows a different way of doing this ?

"Michel Posseth [MCP]" <[email protected]>
schreef
in bericht
Hello Stephany ,

Dim decval As Decimal = CDec(1.23)

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]" <[email protected]>
wrote
in
messageHello ,
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 ? :-)

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.NumberFormatInfo.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
 
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 said:
Nobody ??

Nobody who knows a different way of doing this ?


"Michel Posseth [MCP]" <[email protected]>
schreef in bericht
Hello Stephany ,

well

Dim decval As Decimal = CDec(1.23)

MsgBox(decval.ToString.Split(CChar(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator))(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 said:
What could be simpler than <variable>.ToString.Split("."c)(1).Length?

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



"Michel Posseth [MCP]" <[email protected]>
wrote in
message 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
 
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/forums/topic.asp?topic_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
 
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.
 
rowe_newsgroups said:
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/forums/topic.asp?topic_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
 
Michel said:
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.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.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)
If pos == -1 Then
Return 0
Else
Return value.Length - pos - 1
End If
 
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.

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 said:
Nobody ??

Nobody who knows a different way of doing this ?


"Michel Posseth [MCP]" <[email protected]>
schreef in bericht
Hello Stephany ,

well

Dim decval As Decimal = CDec(1.23)

MsgBox(decval.ToString.Split(CChar(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator))(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







:

What could be simpler than <variable>.ToString.Split("."c)(1).Length?

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



"Michel Posseth [MCP]" <[email protected]>
wrote in
message 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
 
Göran Andersson said:
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.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)
If pos == -1 Then
Return 0
Else
Return value.Length - pos - 1
End If



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.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)

If pos = -1 Then

Return 0

Else

Return value.Length - pos - 1

End If

End Function



Thank you verry much

michel
 
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 said:
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.

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 said:
Nobody ??

Nobody who knows a different way of doing this ?


"Michel Posseth [MCP]" <[email protected]>
schreef in bericht
Hello Stephany ,

well

Dim decval As Decimal = CDec(1.23)

MsgBox(decval.ToString.Split(CChar(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator))(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







:

What could be simpler than <variable>.ToString.Split("."c)(1).Length?

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



"Michel Posseth [MCP]" <[email protected]>
wrote in
message 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
 
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
 
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.NumberFormatInfo.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 said:
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
 
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 said:
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.NumberFormatInfo.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 said:
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 said:
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.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.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)
If pos == -1 Then
Return 0
Else
Return value.Length - pos - 1
End If
 
Cor said:
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.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator))

With this, I measured a 35% better performance than splitting the string. :)
 
Back
Top