string.Format("{0:n0}") in javascript [duplicate] - c#

This question already has answers here:
Format numbers in JavaScript similar to C#
(17 answers)
Closed 9 years ago.
I'm trying to find an equivalent function to:
string.Format("{0:n0}")
in javascript.
Or in different words, I have a long number 10898502 and i want to display it like this 10,898,502.
Is there an easy way to do it ?
Thanks,

For a pure Javascript solution, I would recommend the function below. It is courtesy of this SO Community Wiki entry: How can I format numbers as money in JavaScript?
Number.prototype.formatMoney = function(c, d, t){
var n = this,
c = isNaN(c = Math.abs(c)) ? 2 : c,
d = d == undefined ? "." : d,
t = t == undefined ? "," : t,
s = n < 0 ? "-" : "",
i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};
console.log(
(1000000.94).formatMoney(1, '.', ',') // 1,000,000.9
);

Related

What Is this C# Code doing in algorithms? [duplicate]

This question already has answers here:
Ternary Operator in C#
(4 answers)
Closed 2 years ago.
encoded Data = (encoded Data | bit Shift Buffer)
This line:
j = j + 1 == key.Length ? 0 : j + 1;
could also be written as:
if ((j+1) == key.Length)
j = 0;
else
j = j+1;

Display string with misencoded characters as "?" C#

I am currently working on an assignment where they asked me to recreate the memory viewer in visualstudio as a standalone program.
The issue I have been running into is the fact that Visual Studio uses some kind of non standard encoding with the extended ascii characterset (which is not in the standard Encoding class in C#).
If I print the string in the console some characters are represented as "?". This is fine and is considered a nonissue, the problem is that whenever I put sxaid string into the listbox for display, it filters out any "?" and just straight up removes them. Is there any way I can somehow make sure that either it displays a "?" or that I can fix the encoding?
Here are some screenshots from VS and my program as well as the console output
VS
My program
Console Output
EDIT: Woops I forgot to post the code...
public unsafe void FillForm(IntPtr startAddress)
{
listBox1.Items.Clear();
listBox2.Items.Clear();
listBox3.Items.Clear();
ReadMemory read = new ReadMemory();
List<MemoryModel> memoryModels = new List<MemoryModel>();
byte[] mem = read.ReadAddress(startAddress, DisplaySize);
char[] memChars = new char[mem.Length];
//dataGridView1.Columns.Add("0", "Address");
for (int i = 0; i < DisplaySize; i++)
{
//dataGridView2.Columns.Add(i.ToString(), i.ToString());
//dataGridView3.Columns.Add(i.ToString(), i.ToString());
}
for (int i = 0; i < DisplaySize; i++)
{
//listBox2.Items.Add();
//dataGridView3.Rows.Add();
listBox1.Items.Add("0x" + (startAddress + DisplaySize * i).ToString("X8"));
//dataGridView1[0, i].Value =
MemoryModel model = new MemoryModel();
for (int j = 0; j < DisplaySize; j++)
{
//memChars[j] = mem[j] == 255 || mem[j] == 127 || mem[j] < 33 ? '.' : (char)mem[j];
memChars[j] = mem[j] == 255 || mem[j] == 127 || mem[j] < 33 || ((char)mem[j]).ToString().Contains("\\u")? '.' : (char)mem[j];
Console.WriteLine(memChars[j] + " + " + mem[j]);
model.ByteStringAccordingToSize += mem[j].ToString("X2") + " ";
}
//convert to model
model.CharRepresentation = new string(memChars);
model.FirstByteAddress = "0x" + (startAddress + DisplaySize * i).ToString("X10");
Console.WriteLine("============ \n ROW: " + i);
Console.WriteLine(model.CharRepresentation);
Console.WriteLine("============");
memoryModels.Add(model);
mem = read.ReadAddress(startAddress + DisplaySize * (i + 1), DisplaySize);
}
foreach (var memoryModel in memoryModels)
{
listBox2.Items.Add(memoryModel.ByteStringAccordingToSize);
listBox3.Items.Add(memoryModel.CharRepresentation);
}
}

What is = i < 10 ? "0" + Convert.ToString(i) : Convert.ToString(i) - C# [duplicate]

This question already has answers here:
Benefits of using the conditional ?: (ternary) operator
(17 answers)
Closed 4 years ago.
Never seen something like this before. Specifically line 4.
Currently I understand:
days array at position i-1 gets the value...
I don't know anything beyond the = sign excluding the concatenation from the +.
public String[] months() {
String[] days = new String[12];
for (int i = 1; i <= 12; i++) {
days[i - 1] = i < 10 ? "0" + Convert.ToString(i) : Convert.ToString(i);
}
return days;
}
Also why are there 2 converts?
Looking further into the other code I think the developer copied and pasted previous code. Days array should be months probably, as there are 12 months.
Solved
Thanks, never seen ternary operators before. Thanks!
public String[] months() {
String[] months = new String[12];
for (int i = 1; i <= 12; i++) {
/* faster way of saying */
/* if i < 10 {} else {} */
/* if condition is met do everything before the :, else do everything after */
/* checks for 10 because months can be 1 or 2 digits, 0-9, then 10-12 */
/* single digits concatenated to the 0 */
/* double digits replace the 0 */
months[i - 1] = i < 10 ? "0" + Convert.ToString(i) : Convert.ToString(i);
}
return months;
}
This is the conditional operator, also known as the ternary conditional operator.
It's shorthand for...
if (i < 10)
{
days[i - 1] = "0" + Convert.ToString(i);
}
else
{
days[i - 1] = Convert.ToString(i);
}
The code is basically prepending a "0" to the front of single digit numbers.
It's the ternary operator. If the expression in front of the ? sign is true, the result will be the value after the ? sign, otherwise the value after the colon.
This code adds a leading zero to the value. If the value is less than 10, the value becomes "0" + the value, so 9 becomes "09" etc.
?: is shorthand
IF i < 10
days[i-1] = "O" + Convert.ToString(i)
ELSE
days[i-1] = Convert.ToString(i)

What does ":" do in this exact situation? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# ?: Conditional Operator
Could someone explain me what does ":" do in this situation?
var skupaj = dni + zacetniDan + (((dni + zacetniDan) % 7 != 0) ? 7 - ((dni + zacetniDan) % 7) : 0);
: is part of a Ternary Operator. It is shortcode for an if/else clause.
Example:
int a = b > 5 ? 2 : 3;
is the same as:
int a = 0;
if (b > 5)
{
a = 2;
}
else
{
a = 3;
}
It's a ternary operator.
It is shorthand for the following equivalent of your code:
int skupaj = dni + zacetniDan;
if ((dni + zacetniDan) % 7 != 0) {
skupaj += 7 - ((dni + zacetniDan) % 7);
}
else {
skupaj += 0;
}
? : is conditional operator short form for if / then / else
The first part is condition should be evaluated to boolean its before the ? The expression after ? is then part and is returned when condition is ture and the expression after : is else part and is returned when condition is evaluated to false
(((dni + zacetniDan) % 7 != 0) ? 7 - ((dni + zacetniDan) % 7) : 0);
This is a ternary expression Condition?Expr1:Expr2
The result of the expression is the result of Expr1 if Condition is true and the result of Expr2 otherwise.
In your particular case condition is
((dni + zacetniDan) % 7 != 0)
If this condition is true, the result of the ternary subexpression will be
7 - ((dni + zacetniDan) % 7)
Otherwise 0.
Ternary operator. It allows you to treat a conditional value as a single value.
here's a rudimentary example converting a boolean to a string
string str = myBool ? "true" : "false";
which is equivalent to
string str
if(myBool)
str = "true";
else
str = "false";
Thats the ternary operator: http://msdn.microsoft.com/en-us/library/ty67wk28(v=vs.80).aspx
if (dni + zacetniDan) % 7 != 0) is true then evaluate to 7 - ((dni + zacetniDan) % 7) else evaluate to 0
You can easily understand a line with ? and : by splitting in 3.
The term on the left of ? is the condition
The term between ? and : is what should be used if the condition is true
The term after : is what should be used if the condition is false

Help converting some classic asp code to c# (.net 3.5)

I have this block of code in a .asp file that I am struggling to convert to c#... can anyone help me?
Function EncodeCPT(ByVal sPinCode, ByVal iOfferCode, ByVal sShortKey, ByVal sLongKey)
Dim vob(2), encodeModulo(256), decodeX, ocode
decodeX = " abcdefghijklmnopqrstuvwxyz0123456789!$%()*+,-.#;<=>?[]^_{|}~"
if len(iOfferCode) = 5 then
ocode = iOfferCode Mod 10000
else
ocode = iOfferCode
end if
vob(1) = ocode Mod 100
vob(0) = Int((ocode-vob(1)) / 100)
For i = 1 To 256
encodeModulo(i) = 0
Next
For i = 0 To 60
encodeModulo(asc(mid(decodeX, i + 1, 1))) = i
Next
'append offer code to key
sPinCode = lcase(sPinCode) & iOfferCode
If Len(sPinCode) < 20 Then
sPinCode = Left(sPinCode & " couponsincproduction", 20)
End If
'encode
Dim i, q, j, k, sCPT, s1, s2, s3
i = 0
q = 0
j = Len(sPinCode)
k = Len(sShortKey)
sCPT = ""
For i = 1 To j
s1 = encodeModulo(asc( mid(sPinCode, i, 1)) )
s2 = 2 * encodeModulo( asc( mid(sShortKey, 1 + ((i - 1) Mod k), 1) ) )
s3 = vob(i Mod 2)
q = (q + s1 + s2 + s3) Mod 61
sCPT = sCPT & mid(sLongKey, q + 1, 1)
Next
EncodeCPT = sCPT
End Function
What you have here seems to be pretty standard VBScript code.
Perhaps you could look at some C# tutorial to get the basics or maybe go for VB.NET instead of C#.
The syntax is pretty much the same as VBScript, but remember, the .NET framework is object oriented so some feature or functions are not implemented the same way.
For example, if you want to get the length of a string, you would be using myString.Length instead of Len(myString).
Here are a few C# and VB.NET tutorials for you to look at.
http://www.csharp-station.com/Tutorial.aspx
http://www.csharpkey.com/csharp/Lesson01.htm
http://www.programmersheaven.com/2/VB-NET-School
http://www.homeandlearn.co.uk/net/vbnet.html

Categories

Resources