Double ToString keeping "en-US" format - javascript

Sounds simples - And I know it is... but i'm having issues with it and don't know why exactly..
I have a web application globalized (multilanguages).
When I click to change the language, this is my action:
public ActionResult ChangeCulture(string lang)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
var languageCookie = new HttpCookie("_language") { Value = lang, Expires = DateTime.UtcNow.AddYears(1) };
Response.Cookies.Remove("_language");
Response.SetCookie(languageCookie);
return Redirect(Request.UrlReferrer.PathAndQuery);
}
And I have a page to display some char (I'm using chart.js) and I need to bind a List<double> to a javascript array.
So this list looks like:
var list = new List<double> {144, 0, 540.23};
And I need a simples array in javascript:
var arr = [144, 0, 540.23];
Here is how I'm doing (razor):
var arr = [#string.Join(",", Model.ListWithDoubles.Select(x => Convert.ToString(x, new CultureInfo("en-US"))))]
The problem is:
When I'm using english language its works pretty. The others langues gives me integer numbers instead...
var arr = [144, 0, 540.23]; //en-US
var arr = [144, 0, 54023]; //pt-BR
var arr = [144, 0, 54023]; //it
var arr = [144, 0, 54023]; //es
Questions
Why?
How to fix?

Because in some other non en-US cultures the , and . have the exact opposite meaning and usage. If you are not displaying this data, only for chart purposes, then use CultureInfo.InvariantCulture when converting the double to string representation for the HTML. You should only convert to a culture specific string at the point you want to actually visually display that data value to the user.
var arr = [#string.Join(",", Model.ListWithDoubles.Select(x => Convert.ToString(x, CultureInfo.InvariantCulture)))]
The default format specifier for double is G so it will create output with only a decimal separator. As you want the raw number (not formatted for display) then this (CultureInfo.InvariantCulture) is what you need to pass, not the culture formatted string representation as that is for display only.
To illustrate that the code I posted works regardless of the culture of the current thread here is that code. Drop this into a console application and replace the Main method and run it. You will see that this works. Your issue lies elsewhere, not with this code.
static void Main(string[] args)
{
var cultures = new[] {"en-US", "pt-BR", "it", "es"};
var list = new List<double> {144, 0, 540.23};
Console.WriteLine("Without specifying a culture");
foreach (var culture in cultures.Select(isoCulture => new CultureInfo(isoCulture)))
{
System.Threading.Thread.CurrentThread.CurrentCulture = culture;
System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
Console.WriteLine("Culture: " + culture.Name);
Console.WriteLine("Not defined: " + string.Join(",", list.Select(x => Convert.ToString(x))));
Console.WriteLine("CultureInfo.InvariantCulture: " + string.Join(",", list.Select(x => Convert.ToString(x, CultureInfo.InvariantCulture))));
}
Console.ReadLine(); // stops so you can see the results
}

Related

I'm making a level maker with a preview from the code generated by the level maker, how could I do this?

I have made a Level Generator. Basically there is a 3 by 20 grid of squares, and you can select either one and depending on what you select when you click the button to generate it generate some code for you into a variable (string). It works perfectly but how am I going about to then get that generated code and turn it into a preview.
So far I found out using the keyword eval() but this only does the last line? Any ideas?
Here is a picture of what the layout is:
The Website So Far
The code that I originally had to detect the code was:
if (container.childNodes[index].innerText == 'pos3') {
if (container.childNodes[index].id == '1 second') {
text = text + '\n' + 'cube1 = new theCubeCreator(pos3, 0, 2, 1000),'
//amtselected = amtselected + 1
}....
Something like that then I would bundle it all up with this
var pos1 = 125 //middle
var pos2 = 70 //left
var pos3 = 180 //right
text = text + '\n' + 'cube1 = new theCubeCreator(pos2, 0, 2, 1000)' // Must add this to make it a end
var evaluation = eval(text)
console.log(evaluation);
Sorry if this is hard to read or if you want me to send examples.
What you are doing is bad practice. You should make an object that holds the functions and a generator function that converts and object into a function.
// object for the function / class you want to run
var data = {
name: "console.log",
isClass: false,
params: ["pos2", 0, 2, 1000],
};
var domParse = {
name: "DOMParser",
isClass: true,
params: ["<div>Hello World</div>", "text/html"],
};
var funcs = {
DOMParser: (args) => {
const dom = new DOMParser();
return dom.parseFromString(...args);
},
"console.log": (args) => console.log(...args),
};
// function to generate a function from the object.
function generate({ name, isClass, params }) {
return funcs[name](params);
}
generate(data); // console.log's pos2 0 2 1000
generate(domParse); // creates a DOM object
I took another way than doing eval() (well i still used eval). What I was doing was, to have a variable let text = '' then adding onto that variable doing something like this text = text + \n + '...'
Already this is bad so i took a different approach. all i changed was instead of having a string variable i would have a Array to store my code. Declearing it like this let text = [] then using push() to add the code to it, like so text.push(eval(...)).
Thats what i did to complete to fix my problem if you have any other way or eisier to follow (and yes i know this is messy) then comment (:

Getting the key with largest value from aggregate_histogram result in Google Earth Engine

In Google Earth Engine, I am getting an object obj from an aggregate_histogram call, and print(obj) shows the following:
{
"115.0": 1,
"137.0": 1,
"35.0": 137,
"42.0": 164
}
I would like to extract the key for which the value is largest, so "42.0" (which should please most everyone as the correct answer to any big question).
How can I proceed?
I know how to do it in pure JavaScript, but here it doesn't look like it works:
print(Object.keys(obj)) // yields "[]"
EDIT: adding more info after the first answer by Kosh.
var obj = loc.aggregate_histogram('relativeOrbitNumber_start')
var o = {
"115.0": 1,
"137.0": 1,
"35.0": 137,
"42.0": 164
};
print(o)
print(obj)
print(Object.keys(o))
print(Object.keys(obj))
This yields the following:
screenshot
It seems that it has to do with client- and server-side variables.
So service-side processing steps are required.
Here is something that seems to work:
var o = ee.Dictionary(loc.aggregate_histogram('relativeOrbitNumber_start'))
var okeys = ee.List(o.keys())
var ovals = ee.List(o.values())
var minmax = ee.Dictionary(ovals.reduce(ee.Reducer.minMax()))
var maxval = ee.Number(minmax.get('max'))
var maxind = ovals.indexOf(maxval)
var maxkey = okeys.get(maxind)
print('maxkey:', maxkey)
Unfortunately, this variable then can't be used as input to filterMetadata:
// this yields an empty collection, where maxKey = 42.0:
var sel = loc.filterMetadata('relativeOrbitNumber_start', 'equals', maxKey)
// this yields a non-empty collection:
var sel = loc.filterMetadata('relativeOrbitNumber_start', 'equals', 42.0)
So I first have to get it into the client side by using getInfo, as per this answer.
var ms = maxKey.getInfo() // local string
var mi = parseInt(ms) // local integer
// this yields a non-empty collection:
var sel = loc.filterMetadata('relativeOrbitNumber_start', 'equals', mi)
This seems like a bug though, why wouldn't filterMetadata be able to accept server-side variables as arguments? Especially since using getInfo() is discouraged in the documentation. 🤷
Using reduce:
const o = {
"115.0": 1,
"137.0": 1,
"35.0": 137,
"42.0": 164
};
const max = Object.keys(o).reduce((m, k) => o[m] > o[k] ? m : k)
console.log(max)

Split a JSON array into JS variables - Bixby

So I have a API outputting JSON to my JS code (http://api.opentripmap.com/0.1/ru/places/bbox?lon_min=-123.049641&lat_min=37.550392&lon_max=-122.049641&lat_max=38.550392&kinds=foods&format=geojson&apikey=5ae2e3f221c38a28845f05b685eac8210f10fb196793a9d4f6653c25).
However it contains a JSON Array that looks like this- "coordinates": [ -122.510216, 37.769474 ]
Is there a way to split it into separate JS variables like one variable for the left side and another for the right side. At the moment the array is causing my code to crash as it can only accept one input in each slot.
Sorry If this is a simple question, I haven't been able to solve this yet...
EDIT:
Sorry for the terrible question layout. I have tried to splice and splitting the array with no success (splicing lead to a whole load of undefined errors).
My current code is
module.exports.function = function findLunch(myLocation) {
var loc_long_max = Number(myLocation.longitude) //grab longitude from user
var loc_lat_min = Number(myLocation.latitude) //grab latitude from User
var loc_long_min = loc_long_max - 0.5;
var loc_lat_max = loc_lat_min + 0.5;
var url_all = "http://api.opentripmap.com/0.1/ru/places/bbox?lon_min=" + loc_long_min + "&lat_min=" + loc_lat_min + "&lon_max=" + loc_long_max + "&lat_max=" + loc_lat_max + "&kinds=foods&format=geojson&apikey=5ae2e3f221c38a28845f05b685eac8210f10fb196793a9d4f6653c25"
var results = http.getUrl(url_all, { format: 'json' }); //gets json info from url_all.
console.log(results.features[rand].properties.name)
//console.log(results.feautres[rand].properties.rate)
console.log(results.features[rand].geometry.coordinates)
//console.log(results);
for (var i = rand; i < results.features.length; i++) {
console.log(results.features[rand].properties.name)
console.log(results.features[rand].properties.rate)
var businesses = {
name: results.features[rand].properties.name,
rating:results.features[rand].properties.rate,
coordinates: results.features[rand].geometry.coordinates
}
}
return businesses
So the coordinates need to be split and then be in the businesses var which is then outputted to Bixby....
EDIT 2 : Fixed it - thanks everyone for the help!
Thanks!
Not sure if this is smth you're asking but you can use destructuting assignment to assign items in an array to variables:
const coordinates = [ -122.510216, 37.769474 ];
const [coordinateOne, coordinateTwo] = coordinates;
console.log(coordinateOne) // -122.510216
console.log(coordinateTwo) // 37.769474
You can use destructuring to assign the first and second elements of the array to variables in a simple way:
const coordinates = [-122.510216, 37.769474];
const [left, right] = coordinates;
console.log(left);
console.log(right);

How to convert arrays to objects in javascript?

How could I rewrite this code to object javascript. Since Array usage is prohibed, I can only use objects here. Insted of pushing values to array, I would like to push this values into objects.
var container = [];
document.addEventListener("submit", function(e){
e.preventDefault();
});
window.addEventListener("load",function(){
var submit = document.getElementsByClassName("btn-primary");
submit[0].addEventListener("click",add,false);
document.getElementById("pobrisi").addEventListener("click",deleteAll,false);
var dateElement = document.getElementById('datum');
dateElement.valueAsDate = new Date();
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
if(dd<10){
dd='0'+dd
}
if(mm<10){
mm='0'+mm
}
today = yyyy+'-'+mm+'-'+dd;
dateElement.setAttribute("min",today);
});
function add() {
var title = document.getElementById("title").value;
var type = document.getElementById("type").value;
var datum = document.getElementById("datum").value.split("-");
datum = datum[2]+". "+datum[1]+". "+datum[0];
var data = new Book(title,type,datum);
container.push(data.add());
display();
}
function display(data) {
var destination = document.getElementById("list");
var html = "";
for(var i =0;i <container.length; i++) {
html +="<li>"+container[i]+"</li>";
}
destination.innerHTML = html;
}
function deleteAll(){
container=[];
document.getElementById("list").innerHTML="";
}
Wondering if is possible to write this code whitout any array usage.
initial remarks
The problem here, in my estimation, is that you haven't learned the fundamentals of data abstraction yet. If you don't know how to implement an array, you probably shouldn't be depending on one quite yet. Objects and Arrays are so widespread because they're so commonly useful. However, if you don't know what a specific data type is affording you (ie, what convenience does it provide?), then it's probable you will be misusing the type
If you take the code here but techniques like this weren't covered in your class, it will be obvious that you received help from an outside source. Assuming the teacher has a curriculum organized in a sane fashion, you should be able to solve problems based on the material you've already covered.
Based on your code, it's evident you really have tried much, but why do you think that people here will come up with an answer that your teacher will accept? How are we supposed to know what you can use?
a fun exercise nonetheless
OK, so (we think) we need an Array, but let's pretend Arrays don't exist. If we could get this code working below, we might not exactly have an Array, but we'd have something that works like an array.
Most importantly, if we could get this code working below, we'd know what it takes to make a data type that can hold a dynamic number of values. Only then can we begin to truly appreciate what Array is doing for us.
// make a list
let l = list(1) // (1)
// push an item on the end
l = push(l, 2) // (1 2)
// push another item on the end
l = push(l, 3) // (1 2 3)
// display each item of the list
listeach(l, function (x) {
console.log(x)
})
// should output
// 1
// 2
// 3
runnable demo
All we have to do is make that bit of code (above) work without using any arrays. I'll restrict myself even further and only use functions, if/else, and equality test ===. I see these things in your code, so I'm assuming it's OK for me to use them too.
But am I supposed to believe your teacher would let you write code like this? It works, of course, but I don't think it brings you any closer to your answer
var empty = function () {}
function isEmpty (x) {
return x === empty
}
function pair (x,y) {
return function (p) {
return p(x,y)
}
}
function head (p) {
return p(function (x,y) {
return x
})
}
function tail (p) {
return p(function (x,y) {
return y
})
}
function push (l, x) {
if (isEmpty(l))
return list(x)
else
return pair(head(l), push(tail(l), x))
}
function list (x) {
return pair(x, empty)
}
function listeach (l, f) {
if (isEmpty(l))
return null
else
(f(head(l)), listeach(tail(l), f))
}
// make a list
let l = list(1) // (1)
// push an item on the end
l = push(l, 2) // (1 2)
// push another item on the end
l = push(l, 3) // (1 2 3)
// display each item of the list
listeach(l, function (x) {
console.log(x)
})
closing remarks
It appears as tho you can use an Object in lieu of an Array. The accepted answer (at this time) shows a very narrow understanding of how an object could be used to solve your problem. After this contrived demonstration, are you confident that you are using Objects properly and effectively?
Do you know how to implement an object? Could you fulfill this contract (below)? What I mean by that, is could you write the functions object, set, and get such that the following expressions evaluated to their expected result?
In case it's not obvious, you're not allowed to use Object to make it happen. The whole point of the exercise is to make a new data type that you don't already have access to
m = object() // m
set(m, key, x) // m
get(m, key) // x
set(m, key2, y) // m
get(m, key2) // y
set(m, key3, set(object(), key4, z)) // m
get(get(m, key3), key4) // z
I'll leave this as an exercise for you and I strongly encourage you to do it. I think you will learn a lot in the process and develop a deep understanding and appreciation for what higher-level data types like Array or Object give to you
Since this is a homework I feel like I shouldn't solve it for you, but rather help you in the right direction.
Like Slasher mentioned you can use objects
With JavaScript object one book would look something like
const book = {
title: 'my awesome title',
type: 'novel'
};
book is the object
title is a property with a value 'my awesome title'
type is a property with a value 'novel'
But objects can also have other objects as values. Something like
const BookShelf= {
Book1: {
Title: 'my awesome title',
Type: 'novel'
},
Book2: {
Title: 'my horrible title',
Type: 'sci-fi'
}
};
You can reference the books in the bookshelf in two ways
const book1 = BookShelf.Book1 // Returns the book1 object
const title1 = Book1.Title; // Get the title
const sametitle = BookShelf.Book1.Title // Returns title for book1, same as above.
You can also use brackets:
const book1 = BookShelf['Book1'];
const title1 = BookShelf['Book1']['Title];
You can even make new properties on a object like this:
const Book3 = {
Title: 'running out of ideas'
Type: 'memoir'
};
BookShelf['Book3'] = Book3;
Now the BookShelf has a Book3 property. So your BookShelf object looks like
const BookShelf= {
Book1: {
Title: 'my awesome title',
Type: 'novel'
},
Book2: {
Title: 'my horrible title',
Type: 'sci-fi'
},
Book3 = {
Title: 'running out of ideas'
Type: 'memoir'
};
};
That should get you started :)
JavaScript Objects is a good way to go
1- define a new object:
var myVar = {};
or
var myVar = new Object();
2- usage
// insert a new value, it doesn't matter if the value is a string or int or even another object
// set a new value
myVar.myFirstValue="this is my first value";
// get existing value and do what ever you want with it
var value = myVar.myFirstValue

Turn string from JSON array into another array in javascript

I'm trying to port the following code to use JSON files http://malamas.com/jsuc/jsuc.html
Currently i have managed to take the 'unit' and 'factor' parts of this:
property[0] = "Acceleration";
unit[0] = new Array("Meter/sq.sec (m/sec^2)", "Foot/sq.sec (ft/sec^2)", "G (g)", "Galileo (gal)", "Inch/sq.sec (in/sec^2)");
factor[0] = new Array(1, .3048, 9.806650, .01, 2.54E-02);
and put them into a JSON line:
var text = '{"Accelleration": { "unit":"\\"Meter/sq.sec (m/sec^2)\\", \\"Foot/sq.sec (ft/sec^2)\\", \\"G (g)\\", \\"Galileo (gal)\\", \\"Inch/sq.sec (in/sec^2)\\"", "factor":"1, .3048, 9.806650, .01, 2.54E-02"}}'
var obj = JSON.parse(text);
Which i can have output as a string like this
"Meter/sq.sec (m/sec^2)", "Foot/sq.sec (ft/sec^2)", "G (g)", "Galileo (gal)", "Inch/sq.sec (in/sec^2)"
but i want to be able to feed it into the top code by doing this:
property[0] = "Acceleration";
unit[0] = new Array(obj.Accelleration.unit);
factor[0] = new Array(obj.Accelleration.factor);
Unfortunately, when i do that, unit[0][0] brings up the same thing;
"Meter/sq.sec (m/sec^2)", "Foot/sq.sec (ft/sec^2)", "G (g)", "Galileo (gal)", "Inch/sq.sec (in/sec^2)"
How can i make it so it works as an array? Thanks!
What you have in your JSON is a string that contains some representation of strings. As that representation is not JSON, you can't parse it directly (but you could by adding brackets around it to turn it into JSON).
Instead of having a string that contains strings, make it an array that contains strings. Then when you parse it, you will already have the array:
var text = '{"Accelleration": { '+
'"unit": ["Meter/sq.sec (m/sec^2)", "Foot/sq.sec (ft/sec^2)", "G (g)", "Galileo (gal)", "Inch/sq.sec (in/sec^2)" ], '+
'"factor": [ 1, .3048, 9.806650, .01, 2.54E-02 ]'+
'}}'
var obj = JSON.parse(text);
Now you have your arrays, without having to do anything more than getting them from the object:
unit[0] = obj.Accelleration.unit;
factor[0] = obj.Accelleration.factor;

Categories

Resources