Length of digit after decimal point

S

Stefantastisk

Hey there,

Anyone knows a clever method for knowing the length of the digit after
a decimal point in a standard C# decimal value, WITHOUT use of any
string formatting.

Example:
5231,12231 <- Lengt = 5

Tnx for your attention hope you are capable of helping.

Kind regards
Stefan
 
S

Stefantastisk

Hey John,

Tnx for your reaction.

Your example is one of the most slick methods, that I also have
considered, but there is still a ToString formatting going on.

BUT it seems as, according to reactions in other groups, that it is
nearly impossible to avoid the use of string formatting.

My worries towards the use of string formatting is based on the
differences in cultural string handling and the formatting of decimals
("," or ".").

But thanks for your example.

Take care

Regards,
Stefan

You could just split it apart, without any formatting

Double start_value = 5231.12231;
string[] end_values = start_value.ToString().Split('.');
int slen = end_values[1].Length;
Response.Write(slen);

--
Regards

John Timney (MVP)
VISIT MY WEBSITE:http://www.johntimney.comhttp://www.johntimney.com/blog



Hey there,
Anyone knows a clever method for knowing the length of the digit after
a decimal point in a standard C# decimal value, WITHOUT use of any
string formatting.
Example:
5231,12231 <- Lengt = 5
Tnx for your attention hope you are capable of helping.
Kind regards
Stefan- Skjul tekst i anførselstegn -- Vis tekst i anførselstegn -
 
J

John Timney \(MVP\)

The problem is your asking for a count of the number of integers, in theory
your asking for the length of a set of characters. The fact that they are
numbers is irrelevent really.

Heres an example that doesn't care about whether the seperator is . or , or
whatever, but instead assumes that the first none numeric character of the
string in reverse is the seperator, and thus concludes the char count.

Double myVal = 5231.12231;
char[] reverse = myVal.ToString().ToCharArray();
Array.Reverse(reverse);
for (int i = 0; i < reverse.Length; i++){
if (!char.IsNumber(reverse)){
Response.Write("Length is " + i);
}
}

--
--
Regards

John Timney (MVP)
VISIT MY WEBSITE:
http://www.johntimney.com
http://www.johntimney.com/blog



Hey John,

Tnx for your reaction.

Your example is one of the most slick methods, that I also have
considered, but there is still a ToString formatting going on.

BUT it seems as, according to reactions in other groups, that it is
nearly impossible to avoid the use of string formatting.

My worries towards the use of string formatting is based on the
differences in cultural string handling and the formatting of decimals
("," or ".").

But thanks for your example.

Take care

Regards,
Stefan

You could just split it apart, without any formatting

Double start_value = 5231.12231;
string[] end_values = start_value.ToString().Split('.');
int slen = end_values[1].Length;
Response.Write(slen);

--
Regards

John Timney (MVP)
VISIT MY WEBSITE:http://www.johntimney.comhttp://www.johntimney.com/blog

message

Hey there,
Anyone knows a clever method for knowing the length of the digit after
a decimal point in a standard C# decimal value, WITHOUT use of any
string formatting.
Example:
5231,12231 <- Lengt = 5
Tnx for your attention hope you are capable of helping.
Kind regards
Stefan- Skjul tekst i anførselstegn -- Vis tekst i anførselstegn -
 
S

Stefantastisk

Hey John,

Nice snippet of code, that is a clever design regarding the
delimiter/culture issue.

Tnx mate.

Regards
Stefan

The problem is your asking for a count of the number of integers, in theory
your asking for the length of a set of characters. The fact that they are
numbers is irrelevent really.

Heres an example that doesn't care about whether the seperator is . or , or
whatever, but instead assumes that the first none numeric character of the
string in reverse is the seperator, and thus concludes the char count.

Double myVal = 5231.12231;
char[] reverse = myVal.ToString().ToCharArray();
Array.Reverse(reverse);
for (int i = 0; i < reverse.Length; i++){
if (!char.IsNumber(reverse)){
Response.Write("Length is " + i);
}
}

--
--
Regards

John Timney (MVP)
VISIT MY WEBSITE:http://www.johntimney.comhttp://www.johntimney.com/blog

Hey John,

Tnx for your reaction.

Your example is one of the most slick methods, that I also have
considered, but there is still a ToString formatting going on.

BUT it seems as, according to reactions in other groups, that it is
nearly impossible to avoid the use of string formatting.

My worries towards the use of string formatting is based on the
differences in cultural string handling and the formatting of decimals
("," or ".").

But thanks for your example.

Take care

Regards,
Stefan

You could just split it apart, without any formatting
Double start_value = 5231.12231;
string[] end_values = start_value.ToString().Split('.');
int slen = end_values[1].Length;
Response.Write(slen);
John Timney (MVP)
VISIT MY WEBSITE:http://www.johntimney.comhttp://www.johntimney.com/blog
 
J

John Timney \(MVP\)

glad you like it

--
--
Regards

John Timney (MVP)
VISIT MY WEBSITE:
http://www.johntimney.com
http://www.johntimney.com/blog
Hey John,

Nice snippet of code, that is a clever design regarding the
delimiter/culture issue.

Tnx mate.

Regards
Stefan

The problem is your asking for a count of the number of integers, in
theory
your asking for the length of a set of characters. The fact that they are
numbers is irrelevent really.

Heres an example that doesn't care about whether the seperator is . or ,
or
whatever, but instead assumes that the first none numeric character of the
string in reverse is the seperator, and thus concludes the char count.

Double myVal = 5231.12231;
char[] reverse = myVal.ToString().ToCharArray();
Array.Reverse(reverse);
for (int i = 0; i < reverse.Length; i++){
if (!char.IsNumber(reverse)){
Response.Write("Length is " + i);
}
}

--
--
Regards

John Timney (MVP)
VISIT MY WEBSITE:http://www.johntimney.comhttp://www.johntimney.com/blog

messageHey John,

Tnx for your reaction.

Your example is one of the most slick methods, that I also have
considered, but there is still a ToString formatting going on.

BUT it seems as, according to reactions in other groups, that it is
nearly impossible to avoid the use of string formatting.

My worries towards the use of string formatting is based on the
differences in cultural string handling and the formatting of decimals
("," or ".").

But thanks for your example.

Take care

Regards,
Stefan

You could just split it apart, without any formatting
Double start_value = 5231.12231;
string[] end_values = start_value.ToString().Split('.');
int slen = end_values[1].Length;
Response.Write(slen);
John Timney (MVP)
VISIT MY WEBSITE:http://www.johntimney.comhttp://www.johntimney.com/blog
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top