473,387 Members | 1,757 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,387 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 12386
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.