472,354 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Test for decimal length/size

Hello All,

Does anyone know of a way to test a decimal for the number of places it
has before and after the decimal?

I need to do something like this.

decimal value1 = 172.45;
// If value1 has more then two places before the decimal return zero.
// Or if value1 has more then two places after the decimal return zero.

The reason that I need to do this is that I have a calculation being
done and the result comes out to be something like value1. The value
then is saved to a database column that is a decimal (4,2) which then
causes an error at the database level. I would like to test to decimal
value before this happens.

Thanks,

Aaron

Nov 21 '06 #1
3 12307
mohaaron wrote:
Does anyone know of a way to test a decimal for the number of places it
has before and after the decimal?

I need to do something like this.

decimal value1 = 172.45;
// If value1 has more then two places before the decimal return zero.
// Or if value1 has more then two places after the decimal return zero.

The reason that I need to do this is that I have a calculation being
done and the result comes out to be something like value1. The value
then is saved to a database column that is a decimal (4,2) which then
causes an error at the database level. I would like to test to decimal
value before this happens.
String manipulation is usually the best way to do this. A quick example,
add robustness at your leisure:

decimal value1 = 172.45m;
string [] parts = value1.ToString().Split('.');
if (parts[0].Length 2 || parts[0].Length 2)
return 0;

--
Tom Porterfield

Nov 21 '06 #2
Tom,

Thank you. This gets me going in the right direction.

Regards,

Aaron

On Nov 21, 10:58 am, "Tom Porterfield" <tppor...@mvps.orgwrote:
mohaaron wrote:
Does anyone know of a way to test a decimal for the number of places it
has before and after the decimal?
I need to do something like this.
decimal value1 = 172.45;
// If value1 has more then two places before the decimal return zero.
// Or if value1 has more then two places after the decimal return zero.
The reason that I need to do this is that I have a calculation being
done and the result comes out to be something like value1. The value
then is saved to a database column that is a decimal (4,2) which then
causes an error at the database level. I would like to test to decimal
value before this happens.String manipulation is usually the best way to do this. A quick example,
add robustness at your leisure:

decimal value1 = 172.45m;
string [] parts = value1.ToString().Split('.');
if (parts[0].Length 2 || parts[0].Length 2)
return 0;

--
Tom Porterfield
Nov 22 '06 #3
Hello All,
>
Does anyone know of a way to test a decimal for the number of places it
has before and after the decimal?

I need to do something like this.

decimal value1 = 172.45;
// If value1 has more then two places before the decimal return zero.
// Or if value1 has more then two places after the decimal return zero.

The reason that I need to do this is that I have a calculation being
done and the result comes out to be something like value1. The value
then is saved to a database column that is a decimal (4,2) which then
causes an error at the database level. I would like to test to decimal
value before this happens.

Thanks,

Aaron
Why not use
value1 = Math.Round(value1, 2);
to round the value to two decimal places?

Hans Kesting
Nov 22 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
2
by: MAF | last post by:
How can I test a given string to see if the string is a hex number or an oct number?
3
by: vp | last post by:
Hi all, I have a field AMOUNT(Decimal 13,2) and when I check the length its coming as 7. AMOUNT LENGTH 15.00 7 118.00 7 0.00 7 10.00 7
1
by: ianhen | last post by:
Hi I have a Web Form which I need to get results shown to 2 decimal points only. In the Head I have: - function heatoutput() { var vWidth=document.heatcalculator.roomwidth.value; var...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
8
by: sdlt85 | last post by:
Hi, the program is asking the user for a number ant he base of the number, then it will convert the number to decimal. But is running right only with base 2, 3, 4, 5, 6, 7, 8 but if I try 16 it is...
2
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.