Pulling 2 decimal values from MySQL and trying to use a very simple if statement:
if subtotal < minval then DO THIS else DO THIS
It always thinks subtotal is smaller even though it might not be. IsNumeric confirms both these values are numeric. If I use minval=19.99 instead of pulling from the database it works.
The code for retrieving minval looks like this:
Set rscontrol = db.Execute("select * from websitecontrol ")
minval = FormatNumber(rscontrol("minordervalue"))
FormatNumber is a function for formatting a value for display. It returns a formatted string.
>>> n = 1.25
>>> WScript.Echo TypeName(n)
Double
>>> WScript.Echo TypeName(FormatNumber(n))
String
Never use formatting functions unless the result is intended for being displayed.
Change
minval = FormatNumber(rscontrol("minordervalue"))
to
minval = rscontrol("minordervalue")
If that still doesn't help you can force the value to a double using the CDbl function.
minval = CDbl(rscontrol("minordervalue"))
Beware that the function expects the decimal point as it's configured in your system's regional settings.
Related
I would like to define a datetime type variable that is a result of a simple arithmetic operation between datetime type variables.
I've defined:
datetime duration = ( TimeCurrent() - OrderOpenTime() );
datetime TmStop = StringToTime( "1970.01.01 16:00" );
but when I call it in some other arithmetic operation or generally in code like this
ExitBuy_H1 = ( duration > TmClose && ...
or this
text[3]= "Duration: " + TimeToStr( duration, TIME_MINUTES );
it doesn't work.
TmStop instead works fine.
Does anyone know why?
datetime is a simple integer, number of seconds passed since 1970.01.01 00:00. duration in your example is also in seconds, even though it is datetime formated, when you need it in minutes, divide by 60. TmClose from your example means 16*60*60 seconds and you can compare that integer with any other int of course, but what might be the reason for that?
if you hold you position more then 16 hours, then duration > TmClose is true. if you want to convert difference in seconds (duration) into time, then you will have time converted from 1970.01.01 00:00 + duration seconds.
Anyway it is not clear what is your goal in doing this calculations? if you want to make sure that you hold that particular position more then x hours, then simple bool holdMoreThanXHours = TimeCurrent()-OrderOpenTime()>x*PeriodSeconds(PERIOD_H1), and do not forget to reselect each ticket if you have several ones in open
Fact A) the code, as-is, absolutely out of any question works.
//+------------------------------------------------------------------+
//| Test_StackOverflow.mq4 |
//+------------------------------------------------------------------+
#property strict
void OnStart() {
datetime duration = ( TimeCurrent() - OrderOpenTime() );
string txt = "Duration: " + TimeToStr( duration, TIME_MINUTES );
}
//+------------------------------------------------------------------+
0 error(s), 0 warning(s), compile time: 2000 msec 1 1
Fact B) the full MCVE-context of the code, as-is, is missing.
StackOverflow requires users to post a complete MCVE-representation of the problem. This requirement was not met in the original post.
While the datetime and int data-types are mutually interchangeable, the problem does not seem to be hidden in this intrinsic "duality" of a value representation, but must be somewhere else.
The main suspects for Why? are:
variable definition was masked by another variable having the same name
variable scope-of-definition was exceeded ( asking a variable outside of it's scope )
db.Pool-operations were not preceded by OrderSelect()
When I save my data files, I have a parameter that it is a float, which I want to keep it as a float in the filename. I don't have round up errors, because I define the values of the parameter using
parameters = zeros(Float64, 1000)##50)
iijj = 4.8999
for jjj in 1:1000
iijj += 1/10000
iijj = round(iijj, 4)
parameters[jjj] = iijj
end
and thus every parameter[i] is a float with just 4decimals.
My issue comes when printing the files, I am using
printfile = open("outfile_param$(param).dat" ,"w")
where param=parameters[i]. If I have for example 4.89, I would like to have the name outfile_param4.8900.dat, instead of outfile_param4.89.dat.
I know there are several ways to write in an outputfile, but I would like to keep the format that I have because if not it would be a pain to correct the programs that I work with.
You can use #sprintf to have more precise control over the formatting:
julia> #sprintf("outfile_param%.4f.dat", 4.89)
"outfile_param4.8900.dat"
I am trying to format a zero currency value as an empty string, so that when the currency value is 0.00 then an empty string gets displayed rather than $0.00.
This code is part of an ASP.Net app that will display currency value to end user.
I have used following code to achieve this goal.
Question : Is it possible to achieve this by just using {0:C} format string or another version of this format string instead of using if then else coding for this? If I use ###,###,###.## as the data format string then an empty string shows for zero currency value and also I get rid of the if then else coding but for non-zero values no currency symbol shows.
If Double.Parse(Decimal.Parse(CDec(currencyValue))) = 0 Then
charValue = Nothing
Else
charValue = String.Format("{0:C}", CDec(currencyValue))
End If
UPDATE
I ended up using the following code, which is working fine. If is better than IIf because it does short-circuiting, which means that IIf will evaluate all expressions whether the condition is true or false but If will evaluate the first expression only if condition is true and evaluate the second expression only if condition is false.
Dim d As Decimal
Decimal.TryParse(currencyValue, d)
charValue = If(d = 0D, Nothing, String.Format("{0:C}", d))
I don't think there is a way using formatting to display an empty string.
But you can write it like:
charValue = If( currencyValue = 0D, "", currencyValue.ToString("C") )
using the If Operator (Visual Basic).
Also this is something I would not do:
If Double.Parse(Decimal.Parse(CDec(currencyValue))) = 0 Then
If currencyValue is Decimal:
If (currencyValue = 0D) Then
If currencyValue is Double:
If (currencyValue = 0R) Then
Also, if you are using a database and this is a Sql Server mind SQL Server Data Type Mappings
I don't think you can when using C or the other similar standard formats, since they are already defining a culture-specific format that will include a format for zero.
But if you specify your own custom format, you can specify three different formats separated by ;s, one each for positive numbers, negative numbers, and zero, respectively.
For example (giving an empty string for the zero format, resulting in blank zeroes):
charValue = String.Format("{0:#,##0.00;-#,##0.00;""""}", CDec(currencyValue))
And from what I can see, omitting the format for negative gives a default that matches the positive, whereas omitting the format for zero gives blank, which is what you're looking for, so this should be sufficient as well:
charValue = String.Format("{0:#,##0.00;;}", CDec(currencyValue))
(Using whichever custom format you wish.)
UPDATE: You can get the current currency symbol and manually put it into your custom format. IE:
Dim symbol = CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol
charValue = String.Format("{0}{1:#,##0.00;;}", symbol, CDec(currencyValue))
From the sound of it, though, I think I would actually recommend doing basically what you started with, maybe with an extension method.
<Extension>
Public Function ToCurrencyString(pValue As Decimal) As String
Return IIf(pValue = 0, "", pValue.ToString("C"))
End Function
Dim someValue As Decimal = 1.23
Console.WriteLine(someValue.ToCurrencyString())
This gives you exactly what you're looking for. The exact same format as C gives, but with blank zeroes.
I want to display date in RDLC(Report Definition Language Client-side) as १०/०७/२०१० instead 10/07/2010. How can i do this?
I believe it is not possible - at least not without effort on your side. Native digits are defined in .NET FW like:
var culture = new CultureInfo("km-KH");
for (var i = 0; i < culture.NumberFormat.NativeDigits.Length; i++) {
Console.Out.WriteLine(culture.NumberFormat.NativeDigits[i]);
}
BUT native digits formatting is not supported (yet).
Reference:
http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.digitsubstitution.aspx
The DigitSubstitution property is reserved for future use. Currently,
it is not used in either parsing or formatting operations for the
current NumberFormatInfo object.
You still can manage to do it on your own - i.e. add Report->Report properties->Code
Function Khmer(value)
value = Replace(value,"0","០")
value = Replace(value,"1","១")
value = Replace(value,"2","២")
value = Replace(value,"3","៣")
value = Replace(value,"4","៤")
value = Replace(value,"5","៥")
value = Replace(value,"6","៦")
value = Replace(value,"7","៧")
value = Replace(value,"8","៨")
value = Replace(value,"9","៩")
Khmer = value
End Function
Note - report code uses vb syntax (always) - dunno why.
And in your report add expression for the field you want to encode to Khmer native digits:
=Code.Khmer(Fields!DateCreated.Value)
I am using Linq to entityframework to query some infomration. I am trying to use entityfunction.truncatetime and it doesnt seem to work as expected. here is my sample query
From d In Request
Where d.Requestor= "XXXX" And d.ProcessedFlag = "N"
Select d.RequestID, RequestReason = d.RequestReason.ItemValue, RequestType = d.RequestType.ItemValue, RequestedDate = EntityFunctions.TruncateTime(d.RequestedMoveDate)
The requesteddate doesnt seem to truncate the time part and I am still getting the both Date and time.
Am I missing something here?
In .NET, the DateTime class actually represents both a date and a time. Internally, this is stored as a numeric value represented by the number of 100-nanosecond "ticks" since Midnight, January 1, 1001 AD. This number gets "converted" when it's displayed (either in output or in a debugger). This conversion is done via a format string.
Even if you truncate a DateTime's time portion, it still has a time... it's just 00:00:00, and if you don't want to see that time, you need to adjust your format string to not convert that.
Thus, if you do something like this: DateTime.Now.Date it will display `10/15/2012 00:00:00" if you use the default date conversion string (or whatever is the default format for your culture).
If you want to only display the Date portion, then you must do something like myDate.ToShortDateString() or myDate.ToString("d").
EntityFunctions is a set of tools designed to be used in Linq to Entities queries, because doing DateTime formatting is not normally allowed in a query.
For example, this code does not work:
var q = from x in dc where x.BirthDate == DateTime.Now.AddYears(-15).Date select x;
You have to do it like this:
var q = from x in dc
where x.Birthdate == EntityFunctions.TruncateTime(DateTime.Now.AddYears(-15))
select x;
This will then generate the correct SQL to do date comparisons in SQL code. This is what the EntityFunctions are designed for, not truncating dates in the select portion (although it does work). But, even though the date is truncated, it will still have a Time component, it will just be 00:00:00, and you must use a date format string to present it to your users in the manner you intend.
cant you use ToShortDateString() like below?
List<DateTime> time = new List<DateTime>();
time.Add(DateTime.Now);
var WhatDate = from date in time
select new { Date = date.ToShortDateString() };
In your case try this
From d In Request
Where d.Requestor= "XXXX" And d.ProcessedFlag = "N"
Select new{ RequestID = d.RequestID, RequestReason = d.RequestReason.ItemValue, RequestType = d.RequestType.ItemValue, RequestedDate = d.RequestedMoveDate.ToShortDateString()};