473,382 Members | 1,078 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,382 software developers and data experts.

Please Help ----- Getting wrong result in C# than VB.Net --- Urgent

Hi Everyone:

Please help as i need to write some rounding function in C#. i
Wrote one but does not give me correct result. Can some one please
correct me

C# Codes

################################################## #######################
dblVal = 1234567.89

private int ThreeSifFig(double dblVal)
{
int lngvalue;
int threesigfig;
int lngvaluelen;

lngvalue = (int)(Math.Abs(dblVal));
lngvaluelen = lngvalue.ToString().Length;
if ((lngvaluelen <= 1))
{
threesigfig = 0;
}
else if ((lngvaluelen <= 3))
{
threesigfig = (lngvalue / 10) * 10;
}
else
{
threesigfig = (lngvalue / 10 ^ (lngvaluelen - 3) * 10) ^
(lngvaluelen - 3);
}
if ((dblVal < 0))
{
threesigfig = (threesigfig * -1);
}

return threesigfig;

}
Return Result is 123500 but the expected result is 1230000

################################################## #########################

VB.NET code which return correct result
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim sval As Double = 1234567.89
Dim lngvalue, lngvaluelen As Long
Dim threesigfig As Long
lngvalue = Int(Math.Abs(Val(sval)))
lngvaluelen = Len(CStr(lngvalue))
If lngvaluelen <= 1 Then
threesigfig = 0
ElseIf lngvaluelen <= 3 Then
threesigfig = Int(lngvalue / 10) * 10
Else
threesigfig = Int(lngvalue / 10 ^ (lngvaluelen - 3)) * 10
^ (lngvaluelen - 3)
End If
If sval < 0 Then
threesigfig = threesigfig * -1
End If
End Sub

Result is 1230000 [Correct]

Thanks in advance.
Feb 19 '08 #1
2 1229
On 19 Feb, 21:31, "pargat.si...@gmail.com" <pargat.si...@gmail.com>
wrote:
Hi Everyone:

* * Please help as i need to write some rounding function in C#. i
Wrote one but does not give me correct result. Can some one please
correct me

C# Codes

################################################## #######################
dblVal = 1234567.89

private int ThreeSifFig(double dblVal)
* * {
* * * * int lngvalue;
* * * * int threesigfig;
* * * * int lngvaluelen;

* * * * lngvalue = (int)(Math.Abs(dblVal));
* * * * lngvaluelen = lngvalue.ToString().Length;
* * * * if ((lngvaluelen <= 1))
* * * * {
* * * * * * threesigfig = 0;
* * * * }
* * * * else if ((lngvaluelen <= 3))
* * * * {
* * * * * * threesigfig = (lngvalue / 10) * 10;
* * * * }
* * * * else
* * * * {
* * * * * * threesigfig = (lngvalue / 10 ^ (lngvaluelen - 3)* 10) ^
(lngvaluelen - 3);
* * * * }
* * * * if ((dblVal < 0))
* * * * {
* * * * * * threesigfig = (threesigfig * -1);
* * * * }

* * * * return threesigfig;

* * }

Return Result is 123500 but the expected result is 1230000

################################################## #########################

VB.NET code which return correct result
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

* * * * Dim sval As Double = 1234567.89
* * * * Dim lngvalue, lngvaluelen As Long
* * * * Dim threesigfig As Long
* * * * lngvalue = Int(Math.Abs(Val(sval)))
* * * * lngvaluelen = Len(CStr(lngvalue))
* * * * If lngvaluelen <= 1 Then
* * * * * * threesigfig = 0
* * * * ElseIf lngvaluelen <= 3 Then
* * * * * * threesigfig = Int(lngvalue / 10) * 10
* * * * Else
* * * * * * threesigfig = Int(lngvalue / 10 ^ (lngvaluelen -3)) * 10
^ (lngvaluelen - 3)
* * * * End If
* * * * If sval < 0 Then
* * * * * * threesigfig = threesigfig * -1
* * * * End If
* * End Sub

Result is 1230000 [Correct]

Thanks in advance.
just speculating but what is ^...

testing

int a = 5;
int b = 2;

int c = a ^ b;

c is 7

a 1001
b 0010
c 1001

Aha, use math.pow instead, oh, and that was not much but a tad off...
hope you arent working for a bank company ;)

//CY
Feb 19 '08 #2
On 19 Feb, 23:00, christ...@gmail.com wrote:
On 19 Feb, 21:31, "pargat.si...@gmail.com" <pargat.si...@gmail.com>
wrote:


Hi Everyone:
* * Please help as i need to write some rounding function in C#. i
Wrote one but does not give me correct result. Can some one please
correct me
C# Codes
################################################## #######################
dblVal = 1234567.89
private int ThreeSifFig(double dblVal)
* * {
* * * * int lngvalue;
* * * * int threesigfig;
* * * * int lngvaluelen;
* * * * lngvalue = (int)(Math.Abs(dblVal));
* * * * lngvaluelen = lngvalue.ToString().Length;
* * * * if ((lngvaluelen <= 1))
* * * * {
* * * * * * threesigfig = 0;
* * * * }
* * * * else if ((lngvaluelen <= 3))
* * * * {
* * * * * * threesigfig = (lngvalue / 10) * 10;
* * * * }
* * * * else
* * * * {
* * * * * * threesigfig = (lngvalue / 10 ^ (lngvaluelen - 3) * 10) ^
(lngvaluelen - 3);
* * * * }
* * * * if ((dblVal < 0))
* * * * {
* * * * * * threesigfig = (threesigfig * -1);
* * * * }
* * * * return threesigfig;
* * }
Return Result is 123500 but the expected result is 1230000
################################################## #########################
VB.NET code which return correct result
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
* * * * Dim sval As Double = 1234567.89
* * * * Dim lngvalue, lngvaluelen As Long
* * * * Dim threesigfig As Long
* * * * lngvalue = Int(Math.Abs(Val(sval)))
* * * * lngvaluelen = Len(CStr(lngvalue))
* * * * If lngvaluelen <= 1 Then
* * * * * * threesigfig = 0
* * * * ElseIf lngvaluelen <= 3 Then
* * * * * * threesigfig = Int(lngvalue / 10) * 10
* * * * Else
* * * * * * threesigfig = Int(lngvalue / 10 ^ (lngvaluelen- 3)) * 10
^ (lngvaluelen - 3)
* * * * End If
* * * * If sval < 0 Then
* * * * * * threesigfig = threesigfig * -1
* * * * End If
* * End Sub
Result is 1230000 [Correct]
Thanks in advance.

just speculating but what is ^...

testing

* * * * int a = 5;
* * * * int b = 2;

* * * * int c = a ^ b;

c is 7

a 1001
b 0010
c 1001

Aha, use math.pow instead, oh, and that was not much but a tad off...
hope you arent working for a bank company ;)

//CY- Dölj citerad text -

- Visa citerad text -
oh...

a 101
b 010
c 101
sorry...

aha´, u dont get it... lets try...

int a = 4;
int b = 1;

int c = a ^ b;

will be 5

a= 100
b= 001
c= 101

Oh, its XOR... *smile*

//CY
Feb 19 '08 #3

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

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.