473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

how to get nr of fraction digits

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 2098
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
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
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
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

"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
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
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
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

"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
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
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

"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
>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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ordz | last post by:
I want to write a program that will take a decimal with up to 4 places and convert it to 1/16 ths. I can sort of do that with: n = 375 * 16 / 1000 Print n; "/16" I am planning to use an input...
13
by: Steve | last post by:
I am having trouble finding the answer to this question, I'm thinking the solution must be blindingly obvious, so therefore of course I cannot see it. I wish to take a fraction in DEC say .4 and...
22
by: dos.fishing | last post by:
Hello, I'm writing a function that should do the following: /** * Calculate and return fraction of valueA where max fractions is 31. * param valueA A five bit value, 0-31. * param valueB The...
6
evilmonkey
by: evilmonkey | last post by:
I am very new to programming as well as Java and this is my first post so please forgive me if this is not quite posted correctly. My Problem is that I have only been using scanner to get user input...
1
by: d0ugg | last post by:
Hi, I'm did a fraction program for one of my programming classes and it did compile, however when I'm running the program it crashes for some reason that I do not know. // fraction.cpp ...
2
by: d0ugg | last post by:
Hi, I'm doing a FRACTION program for one of my Programming classes and I'm getting some errors that I can't figure it out. Here is the Assignment: 1. Convert the fraction structure into a...
4
by: d0ugg | last post by:
Hello everyone, I'm creating a program that it is suppose to add, subtract, multiply and also divide fractions and after that, the result has to be reduced to the lowest terms. However, I'm not...
1
by: sony.m.2007 | last post by:
Hi, I have a decimal value and i need to take the digits after decimal places(fraction part only). Is there any simple functions available other than using string functions(searching for '.' and...
10
by: Jason | last post by:
I'm making a program that will convert decimal inputs (in this case, in inches) and output a fractional answer. At the moment, I'm only able to output the fractional answer in three parts: A whole...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.